在数据帧中由几个组计算平均值和SD

时间:2014-12-21 09:16:07

标签: r mean standard-deviation

我有一组数据需要我计算描述性统计数据,例如Mean和SD。诀窍是我尝试使用两个分组变量来计算这些统计信息。最终的目标是显然用SD作为误差条绘制平均值,但我还是留下了最后的结果。我只能计算由1个变量分组的均值和SD。

我已经从我的数据集中添加了一些数据供大家了解我想要实现的目标。我希望为特定治疗的每个变量计算平均值和SD。例如,NBR的平均值和SD分别用于控制,低,中和高,而不是NBR中所有值的均值。

非常感谢任何帮助!

structure(list(Plot = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
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L, 7L, 7L, 7L, 7L, 
    7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 
    11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
    11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
    12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 
    13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 
    14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
    14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
    15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 
    17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
    17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
    18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 
    19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 
    20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
    21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 
    22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L), .Label = c("NDVI", 
    "TCARI", "TCARI.OSAVI", "PRI", "WI", "MSI", "NDWI", "WI.NDVI", 
    "SIPI", "NPQI", "ARI1", "NPCI", "NBR", "BAI", "CSI", "MIRBI", 
    "dr525", "dr720", "dr705", "dr1550", "dr1030", "dr1215"), class = "factor"), 
    value = c(0.49052608, 0.561715025, 0.681058775, 0.418972935, 
    0.598040793, 0.325178174, 0.39611861, 0.482473151, 0.54914233, 
    0.326459616, 0.449550569, 0.374982537, 0.498916873, 0.157797056, 
    0.375598585, 0.369668549, 0.067487106, 0.056591414, 0.043807341, 
    0.051725487, 0.093754202, 0.015804118, 0.010208146, 0.069180561, 
    0.047256836, 0.014956843, 0.046819988, 0.024258394, 0.043112507, 
    0.00452289, 0.016060574, 0.019127195, 0.008477461, 0.006499842, 
    0.002487178, 0.010566068, 0.009558856, 0.002362453, 0.001000077, 
    0.011176193, 0.005692711, 0.001454276, 0.002671122, 0.004389175, 
    0.005963771, 0.000633278, 0.001738936, 0.00297559, -0.077436131, 
    -0.063224907, -0.04141906, -0.066029217, -0.078353929, -0.06269534, 
    -0.062645246, -0.071945342, -0.074503314, -0.039682707, -0.033944239, 
    -0.067220924, -0.068048716, -0.022911819, -0.055515436, -0.058149026, 
    0.954379644, 0.961920174, 0.97662393, 0.94470929, 0.967260471, 
    0.914987902, 0.913176432, 0.97220911, 0.964781397, 0.980127277, 
    0.959769039, 0.950881126, 0.944166816, 0.979386244, 0.936922322, 
    0.955133338, 1.175592718, 1.035411255, 0.800772417, 1.336485183, 
    0.901900085, 1.958346653, 1.803438856, 1.161775069, 0.991080828, 
    1.341931259, 1.367156399, 1.509288183, 1.312893994, 1.906225698, 
    1.55938558, 1.669204336, -0.152945727, -0.133348314, -0.094084016, 
    -0.171933735, -0.120064992, -0.224291293, -0.222158781, -0.118233127, 
    -0.122948258, -0.091499501, -0.158288059, -0.156905855, -0.182074425, 
    -0.146780258, -0.145409057, -0.192813307, 1.945624674, 1.712470081, 
    1.433978924, 2.254821761, 1.617382095, 2.813804784, 2.305310603, 
    2.015053288, 1.756887684, 3.002292558, 2.134952338, 2.535801087, 
    1.892433123, 6.206619232, 2.494477773, 2.583756021, 0.742911497, 
    0.780168303, 0.843457583, 0.65195971, 0.830975899, 0.574818388, 
    0.616816538, 0.719949791, 0.772449293, 0.53750892, 0.731024759, 
    0.612688442, 0.716754763, 0.252652391, 0.578930733, 0.572089182, 
    -0.100561356, -0.099296831, -0.092483347, -0.075723921, -0.142851785, 
    -0.089130121, -0.04298129, -0.102317471, -0.106134004, -0.071568325, 
    -0.037759085, -0.074943034, -0.082143985, -0.013550746, -0.042738224, 
    -0.052687304, 5.676531166, 6.233612201, 7.784421521, 3.641213998, 
    5.227190069, 9.309509369, 20.49898004, 3.561438434, 6.481028072, 
    11.57997948, 5.13213622, 5.522131497, 6.185040981, 7.365686981, 
    13.08585062, 6.613297282, 0.454955786, 0.474862574, 0.426419642, 
    0.416520911, 0.508605292, 0.419544434, 0.392348758, 0.434385238, 
    0.481246028, 0.30321057, 0.306903479, 0.418355778, 0.437338379, 
    0.13683308, 0.344513171, 0.355125174, 0.326893516, 0.387399288, 
    0.515154218, 0.182065945, 0.500676583, -0.209412435, -0.076292248, 
    0.241900919, 0.365695503, -0.114812299, 0.054850362, -0.014567095, 
    0.236467347, -0.44330064, -0.169288639, -0.063890848, 9.714595654, 
    8.915965336, 13.82459669, 6.675855319, 6.569665271, 21.75446394, 
    36.33854707, 6.696758415, 8.98382931, 38.25326354, 30.84745383, 
    11.95769473, 9.717936813, 42.50114638, 30.2370565, 16.44987732, 
    0.937570572, 1.067734668, 1.381944935, 0.81371232, 1.234718748, 
    0.537791913, 0.594631013, 0.919821798, 1.108794746, 0.775895297, 
    0.775232557, 0.698743732, 0.835058861, 0.53024459, 0.671650699, 
    0.632847585, 0.759036354, 0.783179738, 1.238926572, 0.505364128, 
    0.516207899, 1.726260299, 1.740690206, 0.673977376, 0.93851288, 
    1.992771034, 1.706586328, 1.351753865, 0.71549755, 2.224957641, 
    1.954708561, 1.480237458, 0.000674694, 0.000664894, 0.000510151, 
    0.000696155, 0.000952195, 0.000223176, 0.000117448, 0.000866633, 
    0.000569965, 0.000152732, 0.000509423, 0.000387613, 0.000515502, 
    5.22e-05, 0.000179509, 0.000274014, 0.001953683, 0.002430971, 
    0.002398101, 0.002139202, 0.003496132, 0.000307836, 0.000251248, 
    0.002999544, 0.002325187, 0.000269912, 0.000755669, 0.001121944, 
    0.001959135, 0.000112208, 0.000368882, 0.000816404, 0.002392897, 
    0.002322436, 0.00184051, 0.002173463, 0.003689693, 0.000462046, 
    0.000309551, 0.002912204, 0.002086132, 0.00041697, 0.001201466, 
    0.001087332, 0.001915154, 0.000122092, 0.000485394, 0.000828789, 
    0.000452513, 0.000597348, 0.000447671, 0.000567325, 0.000644769, 
    0.000219354, 0.000211073, 0.000596602, 0.000469899, 9.16e-05, 
    0.000291864, 0.000349806, 0.00054511, 6.09e-05, 0.000205434, 
    0.000265793, 0.000319714, 0.000274149, 0.000279715, 0.000402482, 
    0.000500617, 0.000228484, 9.38e-05, 0.000318863, 0.000378217, 
    2.97e-05, 0.000130812, 0.000221053, 0.000385489, 9.62e-06, 
    6.79e-05, 0.000215798, 0.00034812, 0.000394904, 0.000240183, 
    0.000427487, 0.000415141, 0.000205784, 0.000123726, 0.000314875, 
    0.000378047, 5.26e-05, 0.000105128, 0.000218972, 0.000400508, 
    4.07e-05, 8.06e-05, 0.000233541)), .Names = c("Plot", "Landcover", 
"Epoch", "Index_Name", "value"), row.names = c(NA, -352L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

mean(df$value[df$Plot == 'control' & df$Index_Name == 'NBR'])
summary(df$value[df$Plot == 'control' & df$Index_Name == 'NBR'])

是你想要的吗?

df$Plot == 'control' & df$Index_Name == 'NBR'  可以选择控制'和' NBR'

它返回逻辑变量,因此df$value[df$Plot =='control' & df$Index_Name == 'NBR']可以选择NBR和控制的值。

类似地,

mean(df$value[df$Plot == 'low' & df$Index_Name == 'NBR'])
mean(df$value[df$Plot == 'medium ' & df$Index_Name == 'NBR'])
mean(df$value[df$Plot == 'high' & df$Index_Name == 'NBR'])