Rechlay
Rechlay

Reputation: 1517

Output the sd in a single cell in the data.frame

I'd like to calculate the sd of the 4-5 values in the data. That's my data:

> dput(head(tbl_mut1[,-1]))
structure(list(timetE1_1rerun = c(6692.50136510743, 41682.9111356503, 
405946.374877924, 4640.34876265179, 240390.441186252, 703123.176341707
), timetE2_1 = c(14404.8414547167, 40466.9047986558, 638019.540242027, 
2397.71968447607, 393232.499257675, 1197307.39205423), timetE2_1rerun = c(10146.3608040476, 
34148.4389867747, 459639.431186888, 10490.8359468475, 288806.94715668, 
975519.82880571), timetE3_1 = c(5951.82142983062, 45616.664822506, 
658598.827475944, 3961.96726959746, 343020.546938455, 909826.309486319
), timetE4_1 = c(4048.60493244488, 45729.9856907985, 639686.154465906, 
4466.15324797652, 304709.302766716, 863215.201742023), timetE1_2 = c(59094.47562632, 
139889.207295753, 1764684.16499658, 26250.3247678529, 229006.578252829, 
607148.289529371), timetE2_2 = c(27675.589438082, 111309.636536099, 
1117027.2897719, 20320.0802981216, 210671.007373094, 634380.157923317
), timetE3_2 = c(26374.057451648, 129781.168773572, 1147967.44539752, 
18413.5391459416, 215091.483121199, 558724.345729093), timetE4_2 = c(43310.0075838041, 
96924.6162599508, 1156442.475223, 29061.2479135303, 181601.087180429, 
509169.534226553), eve_mean = c(7774.44229552491, 43374.1166119026, 
585562.72426545, 3866.54724117546, 320338.197537275, 918368.01990607
), mor_mean = c(39113.5325249635, 119476.157216344, 1296530.34384725, 
23511.2980313616, 209092.538981888, 577355.581852083), tot_mean = c(23443.9874102442, 
81425.1369141232, 941046.53405635, 13688.9226362685, 264715.368259581, 
747861.800879077), tot_meanwte = c(258492586.999527, 258492586.999527, 
258492586.999527, 258492586.999527, 258492586.999527, 258492586.999527
), tot_meanwtm = c(246665241.110832, 246665241.110832, 246665241.110832, 
246665241.110832, 246665241.110832, 246665241.110832), tot_sdwte = c(16743477.4011497, 
16743477.4011497, 16743477.4011497, 16743477.4011497, 16743477.4011497, 
16743477.4011497), tot_sdwtm = c(3922418.43271348, 3922418.43271348, 
3922418.43271348, 3922418.43271348, 3922418.43271348, 3922418.43271348
)), .Names = c("timetE1_1rerun", "timetE2_1", "timetE2_1rerun", 
"timetE3_1", "timetE4_1", "timetE1_2", "timetE2_2", "timetE3_2", 
"timetE4_2", "eve_mean", "mor_mean", "tot_mean", "tot_meanwte", 
"tot_meanwtm", "tot_sdwte", "tot_sdwtm"), row.names = c(NA, 6L
), class = "data.frame")

I skept the first column because there are a lot of names which will bring a tons of code. I used this code to calculate the sd and output in the next column:

tbl3 <- sd(c(tbl_mut1[445,2],tbl_mut1[445,3],tbl_mut1[445,4],tbl_mut1[445,5],tbl_mut1[445,6]))

tbl_mut1["tot_sdwte"] <- NA
tbl_mut1$tot_sdwte <- tbl3

The problem is that I have the result in whole column and I just want to have it in a single "cell" of the data frame.

Upvotes: 0

Views: 56

Answers (2)

IRTFM
IRTFM

Reputation: 263411

If you want to append an informative bit of summary to a data.frame there is the option of making an "attribute":

attr(tbl_mut1, "tot_sdwte") <- tbl3
?attr

That attribute could be anything. (I am wondering about the code you used for tbl3's construction however. It appears you would only have gotten the sd of row 445 for columns 2-6 (six values) and if that was what you wanted, then just use: sd(tbl_mut1[445,2:6])

Upvotes: 0

Christopher Louden
Christopher Louden

Reputation: 7592

Data frames are lists made up of vectors of the same length. These vectors are the columns. That is why when you add a single value to a data frame, it appears as the whole column.

To get around this, you can do something like this:

tbl_mut1$tot_sdwte <- c(tbl3, rep(NA, dim(tbl_mut1)[1] - 1))

This adds a vector with the first value as the standard deviation and the rest of the values are missing.

Upvotes: 1

Related Questions