我有以下数据框。我想获取每种颜色和样本组合的平均值。
mm <-
structure(list(color = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("red", "blue"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("SAMPLE_8",
"SAMPLE_1", "SAMPLE_2", "SAMPLE_3", "SAMPLE_4", "SAMPLE_5", "SAMPLE_6",
"SAMPLE_7"), class = "factor"), value = c(5204975.22195835, 5921431.37016771,
4483287.22007228, 9334512.14362206, 8290545.0546112, 5511308.78905786,
4535669.79559233, 6084482.4859888, 4552670.23553827, 6617188.31470945,
4465820.654524, 4671357.33199838, 311300.931942485, 302632.345972663,
282997.351878223, 537463.470193835, 750993.852296993, 310145.485558992,
318414.131512382, 302279.607384422, 326870.420217549, 337456.764928226,
281702.352478868, 305075.341449454, 1142089.96619646, 1381523.19977651,
1478102.43751732, 2313422.4839489, 1165360.86570532, 1371146.64341499,
1168538.40843253, 1140498.10246773, 3846417.77575045, 3439179.29840624,
5779121.02860911, 3948012.39935442, 5309451.60747925, 6042469.64246727,
4502373.03854667, 9777607.60721765, 8373484.97756233, 5694750.45922109,
4596450.3240182, 6267456.44789408, 4634116.88423172, 6896467.35967418,
4517202.4683853, 4710615.3886163, 324516.034016253, 291133.601851379,
547686.545230872, 343315.425877443, 301950.787630643, 407330.982240814,
1622559.39237951, 293733.354868293, 3402268.54959027, 6405624.51540237,
747380.853508684, 6291676.05179056, 4976273.57530081, 5788712.60586859,
4356126.64775492, 9078023.65858526, 8172714.31466804, 5308793.61183594,
4397207.70605689, 5858088.51971103, 4392449.83869719, 6299277.85075423,
4344945.38910177, 4490871.86656671, 355542.3811289, 315932.775514382,
458535.380638158, 365431.666781714, 357061.223400342, 445932.523728128,
2061248.39168834, 306376.953744205, 3145802.11824005, 7766554.53327765,
397953.420734522, 7536514.52523919, 5204975.22195835, 5921431.37016771,
4483287.22007228, 9334512.14362206, 8290545.0546112, 5511308.78905786,
4535669.79559233, 6084482.4859888, 4552670.23553827, 6617188.31470945,
4465820.654524, 4671357.33199838)), class = "data.frame", row.names = c(NA,
-96L))
看起来像这样(前几行)
color variable value
1 blue SAMPLE_8 5204975.2
2 blue SAMPLE_8 5921431.4
3 red SAMPLE_8 4483287.2
4 blue SAMPLE_8 9334512.1
5 blue SAMPLE_8 8290545.1
6 blue SAMPLE_8 5511308.8
7 red SAMPLE_8 4535669.8
8 blue SAMPLE_8 6084482.5
9 red SAMPLE_8 4552670.2
10 red SAMPLE_8 6617188.3
11 red SAMPLE_8 4465820.7
12 red SAMPLE_8 4671357.3
13 blue SAMPLE_1 311300.9
14 blue SAMPLE_1 302632.3
15 red SAMPLE_1 282997.4
16 blue SAMPLE_1 537463.5
17 blue SAMPLE_1 750993.9
18 blue SAMPLE_1 310145.5
19 red SAMPLE_1 318414.1
20 blue SAMPLE_1 302279.6
21 red SAMPLE_1 326870.4
22 red SAMPLE_1 337456.8
23 red SAMPLE_1 281702.4
24 red SAMPLE_1 305075.3
我想要这样的东西。
color variable value
red SAMPLE_8 xxxxxxx(x being the mean value for red SAMPLE_8( values)
blue SAMPLE_8 xxxxxxx(x being the mean value for blue SAMPLE_8( values)
red SAMPLE_1 xxxxxxx(x being the mean value for red SAMPLE_1( values)
blue SAMPLE_1 xxxxxxx(x being the mean value for blue SAMPLE_1( values)
...
我认为dplyr可能会对这个问题有所帮助,但我希望您提出建议。 知道如何获取请求的输出。
答案 0 :(得分:0)
我找到了dplyr的解决方案。
columns <- c("color","variable")
mm %>%
#group_by(Odor,variable) %>%
group_by_at(vars(one_of(columns))) %>%
summarise(mean = mean(value))