我想计算数据中4-5值的sd。这是我的数据:
> 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")
我怀疑第一列,因为有很多名称会带来大量的代码。 我使用此代码计算下一列中的sd和输出:
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
问题是我的结果是整列,我只想将它放在数据框的单个“单元格”中。
答案 0 :(得分:1)
数据框是由相同长度的矢量组成的列表。这些向量是列。这就是为什么当您向数据框添加单个值时,它将显示为整个列。
要解决这个问题,你可以这样做:
tbl_mut1$tot_sdwte <- c(tbl3, rep(NA, dim(tbl_mut1)[1] - 1))
这会添加一个第一个值作为标准差的向量,其余的值都会丢失。
答案 1 :(得分:0)
如果您想在data.frame中添加一些信息摘要,可以选择制作“属性”:
attr(tbl_mut1, "tot_sdwte") <- tbl3
?attr
该属性可以是任何东西。 (我想知道你用于tbl3构造的代码。看来你只得到第446行sd
的第2-6列(六个值),如果那是你想要的,那么就使用:sd(tbl_mut1[445,2:6])