用dplyr汇总后如何执行计算?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用dplyr汇总后如何执行计算?相关的知识,希望对你有一定的参考价值。

我已经通过以下方式对两组进行了逐组计算:

group_stats <- fev %>%
  group_by(smoking) %>%
  summarize(mean = mean(fev), n = n(), sd = sd(fev), se = sd / sqrt(n))

将产生具有两行的数据帧。现在我要计算以下内容:

  1. 两行之间的均值之差
  2. 类似于se的新se[row_1] ^ 2 + se[row_2] ^ 2值。

是否有整齐的方式?

[dput输出:

structure(list(age = c(8, 13, 8, 11, 8, 8, 9, 11, 15, 8, 6, 8, 
12, 8, 7, 9, 10, 8, 12, 13, 11, 12, 9, 7, 11, 14, 12, 9, 10, 
8, 10, 11, 6, 9, 11, 14, 11, 10, 9, 11, 11, 11, 8, 15, 7, 9, 
11, 11, 8, 8, 9, 11, 8, 11, 8, 8, 9, 14, 9, 10, 16, 13, 16, 10, 
8, 12, 9, 11, 8, 8, 12, 7, 13, 9, 7, 8, 10, 7, 13, 13, 8, 8, 
8, 16, 12, 10, 14, 9, 8, 7, 9, 11, 12, 13, 13, 10, 6, 13, 9, 
7, 6, 8, 6, 12, 11, 10, 11, 9, 10, 9, 13, 10, 12, 6, 13, 13, 
10, 11, 11, 8, 9, 9, 15, 7, 8, 15, 15, 8, 14, 10, 6, 10, 8, 6, 
12, 8, 13, 9, 16, 10, 11, 8, 6, 11, 10, 13, 12, 11, 10, 8, 8, 
10, 12, 10, 7, 9, 9, 12, 8, 7, 8, 9, 6, 11, 13, 8, 10, 12, 9, 
11, 13, 6, 13, 10, 8, 12, 6, 9, 9, 13, 11, 15, 6, 11, 10, 10, 
10, 7, 7, 12, 12, 10, 13, 10, 10, 10, 11, 10, 13, 9, 11, 12, 
10, 8, 11, 11, 10, 13, 13, 9, 8, 11, 9, 7, 10, 11, 9, 9, 11, 
14, 9, 7, 10, 7, 15, 14, 11, 9, 8, 7, 13, 13, 9, 9, 7, 11, 11, 
16, 9, 15, 7, 12, 7, 6, 8, 12, 8, 14, 9, 9, 8, 15, 10, 12, 13, 
11, 8, 11, 8, 11, 13, 8, 11, 9, 6, 10, 11, 10, 11, 9, 8, 12, 
12, 12, 8, 9, 15, 12, 14, 14, 9, 9, 10, 13, 8, 13, 13, 9, 9, 
8, 11, 9, 7, 7, 9, 9, 9, 9, 7, 8, 9, 15, 16, 10, 6, 9, 12, 9, 
8, 8, 8, 9, 11, 7, 15, 7, 11, 12, 8, 11, 14, 7, 6, 8, 7, 12, 
11, 13, 15, 14, 12, 8, 11, 10, 11, 9, 7, 9, 8, 10, 7, 8, 15, 
11, 11, 16, 13, 11, 9, 6, 9, 6, 11, 11, 11, 11, 7, 16, 11, 10, 
8, 7, 11, 10, 8, 14, 6, 14, 10, 12, 6, 13, 9, 7, 11, 12, 10, 
6, 9, 10, 12, 13, 8, 7, 12, 11, 8, 11, 6, 11, 12, 7, 13, 7, 14, 
7, 10, 12, 10, 6, 9, 9, 9, 12, 8, 9, 6, 12, 7, 10, 9, 10, 11, 
11, 7, 10, 7, 10, 10, 16, 6, 6, 6, 10, 14, 12, 7, 10, 13, 11, 
9, 7, 7, 7, 11, 9, 7, 8, 11, 8, 8, 10, 10, 9, 8, 9, 12, 6, 8, 
7, 9, 11, 11, 9, 10, 9, 14, 9, 7, 8, 11, 7, 6, 12, 9, 8, 10, 
13, 10, 13, 15, 8, 9, 10, 15, 11, 9, 9, 13, 14, 6, 6, 10, 10, 
12, 14, 7, 11, 11, 13, 10, 7, 14, 10, 9, 9, 6, 14, 12, 10, 11, 
8, 12, 9, 11, 8, 16, 7, 14, 12, 13, 12, 8, 10, 11, 16, 9, 12, 
13, 10, 10, 11, 10, 10, 13, 9, 9, 14, 10, 9, 16, 9, 6, 11, 6, 
11, 12, 14, 15, 11, 11, 12, 12, 9, 8, 8, 11, 6, 9, 7, 10, 12, 
10, 7, 10, 8, 12, 9, 13, 12, 9, 8, 12, 11, 11, 8, 11, 11, 14, 
9, 16, 9, 6, 10, 10, 12, 15, 11, 8, 15, 8, 9, 7, 9, 10, 9, 8, 
10, 12, 7, 8, 11, 12, 10, 10, 8, 11, 8), fev = c(1.94, 3.785, 
2.123, 2.993, 2.094, 2.187, 2.23, 2.14, 5.793, 2.069, 1.979, 
1.931, 1.916, 1.547, 2.219, 3.135, 1.937, 2.665, 2.971, 2.677, 
3.587, 2.569, 2.973, 1.603, 4.637, 4.273, 2.759, 2.725, 2.435, 
1.624, 1.858, 3.411, 1.713, 1.895, 2.542, 3.428, 2.659, 2.581, 
2.076, 2.633, 2.562, 2.606, 2.335, 3.004, 1.69, 2.348, 2.822, 
2.928, 2.01, 2.42, 2.592, 2.866, 2.215, 2.318, 2.631, 1.703, 
1.969, 2.236, 2.165, 2.391, 2.903, 4.225, 3.387, 3.111, 1.556, 
2.332, 2.851, 3.681, 2.631, 2.531, 3.924, 1.726, 4.305, 2.871, 
1.75, 2.09, 2.341, 2.371, 4.448, 3.208, 1.512, 2.175, 2.5, 2.795, 
2.752, 2.352, 4.683, 2.964, 2.503, 2.55, 2.923, 2.735, 2.241, 
4.336, 3.255, 2.642, 2.262, 3.06, 3, 1.905, 1.691, 2.476, 1.796, 
3.255, 4.324, 1.458, 3.069, 2.717, 3.456, 2.715, 3.906, 2.568, 
3.53, 1.624, 3.549, 3.089, 2.855, 3.847, 1.694, 1.657, 2.182, 
2.65, 2.679, 1.698, 2.069, 4.279, 4.5, 2.927, 3.806, 2.691, 1.747, 
2.358, 1.962, 1.415, 2.499, 2.993, 4.789, 2.485, 4.504, 2.864, 
2.458, 1.292, 1.535, 3.171, 2.328, 3.056, 2.889, 2.689, 2.175, 
2.015, 2.305, 3.183, 4.55, 2.094, 1.649, 1.953, 2.119, 3.341, 
1.844, 1.609, 1.429, 2.639, 1.634, 3.845, 3.078, 2.388, 2.77, 
3.343, 2.487, 2.665, 3.086, 1.697, 3.297, 2.356, 1.759, 2.304, 
1.699, 3.114, 2.463, 2.646, 2.988, 4.506, 1.878, 3.515, 2.292, 
3.086, 4.591, 1.37, 2.093, 2.605, 3.186, 2.688, 3.141, 2.387, 
3.007, 2.673, 2.827, 3.251, 2.704, 1.754, 2.417, 2.417, 3.038, 
2.732, 3.011, 2.498, 3.795, 3.984, 2.434, 2.091, 2.336, 3.247, 
2.57, 2.084, 2.481, 2.812, 2.574, 3.842, 2.362, 4.271, 1.947, 
2.22, 2.364, 2.564, 3.33, 2.276, 2.887, 2.09, 1.872, 1.253, 2.531, 
2.449, 2.803, 1.912, 1.826, 4.593, 3.222, 3.674, 3.223, 2.278, 
2.419, 3.403, 1.624, 1.338, 2.303, 4.831, 1.794, 4.763, 2.196, 
2.164, 2.354, 2.264, 2.891, 2.751, 4.045, 3.596, 2.193, 2.346, 
1.735, 3.058, 3.887, 2.681, 2.387, 2.069, 1.666, 3.329, 3.108, 
2.25, 3.12, 2.301, 1.897, 3.058, 3.519, 3.082, 2.328, 3.004, 
3.799, 3.692, 2.538, 3.957, 2.316, 1.933, 2.696, 2.976, 2.709, 
3.147, 3.994, 2.352, 3.042, 1.758, 2.123, 1.855, 1.79, 2.046, 
2.457, 3.681, 2.259, 2.13, 1.682, 1.697, 2.893, 3.211, 2.608, 
3.489, 1.719, 3.029, 3.231, 2.56, 2.481, 2.458, 2.382, 2.32, 
4.073, 1.461, 3.727, 1.165, 4.065, 3.079, 1.344, 3.32, 3.395, 
1.631, 1.431, 2.226, 1.64, 2.841, 3.022, 4.232, 2.635, 3.741, 
3.835, 2.258, 2.081, 2.201, 2.501, 2.797, 2.578, 1.606, 1.953, 
3.05, 1.92, 2.118, 2.198, 3.47, 3.102, 3.688, 3.297, 2.884, 2.135, 
1.427, 2.798, 1.572, 3.169, 2.386, 2.957, 2.491, 2.135, 2.981, 
2.563, 2.838, 2.673, 2.535, 2.785, 2.216, 2.639, 2.934, 1.65, 
3.68, 3.11, 3.279, 1.715, 5.083, 2.604, 1.473, 2.894, 2.556, 
3.203, 1.348, 2.042, 2.545, 4.073, 3.745, 2.144, 1.658, 2.384, 
3.774, 1.675, 3.206, 1.672, 3.236, 4.72, 2.095, 2.599, 1.728, 
3.436, 1.917, 2.1, 3.501, 3.35, 1.536, 2.166, 2.833, 1.708, 2.935, 
2.016, 2.85, 1.481, 3.035, 1.612, 3.354, 2.442, 3.305, 3.425, 
3.078, 1.742, 3.073, 1.829, 2.901, 2.561, 4.27, 1.718, 1.523, 
1.602, 3.498, 4.309, 3.529, 1.72, 2.246, 3.577, 2.762, 2.46, 
2.366, 1.796, 1.415, 3.104, 1.942, 1.495, 2.071, 2.953, 2.135, 
2.341, 1.873, 2.862, 2.455, 2.358, 2.529, 4.08, 2.104, 1.78, 
1.789, 1.751, 3.28, 2.524, 2.04, 2.287, 2.048, 3.78, 2.988, 1.611, 
1.698, 2.463, 1.827, 1.527, 2.868, 2.076, 2.145, 2.132, 3.331, 
2.758, 2.216, 4.284, 1.744, 2.659, 1.858, 2.887, 3.977, 3.016, 
1.591, 4.877, 3.169, 1.343, 2.102, 2.592, 2.72, 3.222, 3.585, 
1.935, 3.023, 4.13, 2.819, 3.132, 2.002, 4.842, 2.365, 1.886, 
3.239, 1.423, 2.997, 4.411, 2.504, 3.49, 2.98, 4.393, 1.716, 
4.007, 1.987, 4.872, 2.111, 3.074, 3.751, 3.193, 2.913, 1.999, 
1.823, 3.258, 4.07, 2.457, 5.224, 3.816, 2.25, 3.127, 2.354, 
1.665, 2.975, 4.756, 2.246, 2.42, 3.381, 3.2, 1.869, 3.645, 1.558, 
1.338, 2.578, 1.826, 2.17, 3.001, 4.111, 3.985, 3.583, 2.974, 
3.791, 2.752, 2.1, 2.333, 1.335, 3.369, 1.919, 2.588, 2.056, 
2.813, 4.203, 3.09, 2.158, 3.413, 1.991, 2.579, 2.126, 3.152, 
2.347, 2.631, 1.523, 2.714, 3.654, 3.515, 1.562, 3.339, 3.223, 
2.891, 1.773, 4.299, 2.571, 1.695, 3.048, 2.646, 3.082, 3.122, 
2.465, 2.293, 3.731, 2.288, 2.056, 1.877, 2.688, 2.52, 3.556, 
2.004, 3.166, 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1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("ns", "s"), class = "factor")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -598L))
答案

dplyr中,可以通过在[]中插入单个或范围值来引用每一行中的值。例如,mean列的第一个值可以通过mean[1]获得,第二个可以通过mean[2]获得,都可以通过mean[1:2]mean[c(1, 2)]等获得。]

如果只有两行,您还可以通过last(mean)访问第二行,通过first(mean)访问第一行。

当您以前有group_by语句时,这些索引引用每个组的行索引。

但是,执行此操作的最佳方法是仅执行基本操作。例如,您可以计算指数,然后将其sum代入se,而对于diff,只需采用mean函数即可。

见下文:

group_stats <- fev %>%
  group_by(smoking) %>%
  summarize(mean = mean(fev), n = n(), sd = sd(fev), se = sd / sqrt(n)) %>%
  mutate(se_sum = sum(se ^ 2),
         se_idx = se[1] ^ 2 + se[2] ^ 2,
         mean_diff = diff(mean),
         mean_idx = mean[2] - mean[1],
         mean_diffLast = last(mean) - first(mean))

输出:

group_stats

# A tibble: 2 x 10
  smoking  mean     n    sd     se se_sum se_idx mean_diff mean_idx mean_diffLast
  <fct>   <dbl> <int> <dbl>  <dbl>  <dbl>  <dbl>     <dbl>    <dbl>         <dbl>
1 ns       2.61   539 0.788 0.0339 0.0111 0.0111     0.628    0.628         0.628
2 s        3.24    59 0.764 0.0995 0.0111 0.0111     0.628    0.628         0.628

以上是关于用dplyr汇总后如何执行计算?的主要内容,如果未能解决你的问题,请参考以下文章

如何根据使用 dplyr 的组上的聚合函数计算新列(在汇总统计信息上添加汇总统计信息)?

如何按组进行汇总并使用R中的dplyr获取总体数据集的摘要

dplyr 用小计汇总

R dplyr如何通过列号而不是通过汇总的列名选择变量

在 Redshift dplyr 操作中重命名汇总列

如果所有汇总值都是NA,则dplyr汇总保留NA