0

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.

4

2 回答 2

1

数据框是由相同长度的向量组成的列表。这些向量是列。这就是为什么当您将单个值添加到数据框中时,它会显示为整列。

要解决此问题,您可以执行以下操作:

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

这添加了一个向量,其中第一个值作为标准偏差,其余值缺失。

于 2013-11-06T14:47:52.457 回答
0

如果您想在 data.frame 中添加信息丰富的摘要,则可以选择创建“属性”:

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

该属性可以是任何东西。(但是,我想知道您用于构建 tbl3 的代码。看来您只会得到sd第 445 行的第 2-6 列(六个值),如果这是您想要的,那么只需使用:sd(tbl_mut1[445,2:6])

于 2013-11-07T01:02:18.563 回答