计算数据框中每 13 行的平均值

Calculate the mean of every 13 rows in data frame

我有一个包含 2 列和 3659 行的数据框 df

我试图通过对这个数据框中的每 10 行或 13 行进行平均来减少数据集,所以我尝试了以下操作:

# number of rows per group
n=13
# number of groups
n_grp=nrow(df)/n
round(n_grp,0)
# row indices (one vector per group)
idx_grp <- split(seq(df), rep(seq(n_grp), each = n))

# calculate the col means for all groups
res <- lapply(idx_grp, function(i) {
  # subset of the data frame
  tmp <- dat[i]
  # calculate row means
  colMeans(tmp, na.rm = TRUE)
})
# transform list into a data frame
dat2 <- as.data.frame(res)

但是,我无法将行数除以 10 或 13,因为数据长度不是拆分变量的倍数。所以我不确定那该怎么做(我只想计算最后一组的平均值 - 即使元素少于 10 个)

我也试过这个,结果是一样的:

df1=split(df, sample(rep(1:301, 10)))

如果 df 是 data.table,您可以使用 %/% 分组,如

library(data.table)
setDT(df)
n <- 13 # every 13 rows

df[, mean(z), by= (seq(nrow(df)) - 1) %/% n]

如果您想要每第 n 行,请使用 %% 而不是 %/%

df[, mean(z), by= (seq(nrow(df)) - 1) %% n]

这是使用 aggregate()rep() 的解决方案。

df <- data.frame(a=1:12, b=13:24 );
df;
##     a  b
## 1   1 13
## 2   2 14
## 3   3 15
## 4   4 16
## 5   5 17
## 6   6 18
## 7   7 19
## 8   8 20
## 9   9 21
## 10 10 22
## 11 11 23
## 12 12 24
n <- 5;
aggregate(df, list(rep(1:(nrow(df) %/% n + 1), each = n, len = nrow(df))), mean)[-1];
##      a    b
## 1  3.0 15.0
## 2  8.0 20.0
## 3 11.5 23.5

此解决方案处理 nrow(df) 不可被 n 整除的问题的重要部分是指定 len 参数(实际上完整的参数名称是 length.out) 的 rep(),它会自动将组向量限制为适当的长度。

这应该有效。使用 n = 13 将 13 行聚集在一起。如果您有 27 行,您将得到大小为 13、13、1 的组。

n.colmeans = function(df, n = 10){
    aggregate(x = df,
              by = list(gl(ceiling(nrow(df)/n), n)[1:nrow(df)]),
              FUN = mean)
}

n.colmeans(state.x77, 10)

  Group.1 Population Income Illiteracy Life Exp Murder HS Grad Frost     Area
1       1     4892.8 4690.8       1.44   70.508   9.53   53.63  75.1 116163.6
2       2     3570.5 4419.4       1.12   71.110   7.07   53.35  99.8  44406.6
3       3     3697.9 4505.5       0.93   70.855   6.64   55.25 131.7  60873.0
4       4     5663.9 4331.2       1.33   70.752   7.12   49.59 103.6  56949.5
5       5     3407.0 4232.1       1.03   71.168   6.53   53.72 112.1  75286.7

dplyr 方式

n1 <- 10
iris %>% group_by(mean = (row_number() -1) %/% n1) %>%
  mutate(mean = mean(Sepal.Length))

# A tibble: 150 x 6
# Groups:   mean [15]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  mean
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa   4.86
 2          4.9         3            1.4         0.2 setosa   4.86
 3          4.7         3.2          1.3         0.2 setosa   4.86
 4          4.6         3.1          1.5         0.2 setosa   4.86
 5          5           3.6          1.4         0.2 setosa   4.86
 6          5.4         3.9          1.7         0.4 setosa   4.86
 7          4.6         3.4          1.4         0.3 setosa   4.86
 8          5           3.4          1.5         0.2 setosa   4.86
 9          4.4         2.9          1.4         0.2 setosa   4.86
10          4.9         3.1          1.5         0.1 setosa   4.86
# ... with 140 more rows

或者如果 n1 不是除数或 nrow(df) 那么也

n1 <- 7

# A tibble: 150 x 6
# Groups:   mean [21]
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  mean
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
 1          5.1         3.5          1.4         0.2 setosa    4.9
 2          4.9         3            1.4         0.2 setosa    4.9
 3          4.7         3.2          1.3         0.2 setosa    4.9
 4          4.6         3.1          1.5         0.2 setosa    4.9
 5          5           3.6          1.4         0.2 setosa    4.9
 6          5.4         3.9          1.7         0.4 setosa    4.9
 7          4.6         3.4          1.4         0.3 setosa    4.9
 8          5           3.4          1.5         0.2 setosa    4.8
 9          4.4         2.9          1.4         0.2 setosa    4.8
10          4.9         3.1          1.5         0.1 setosa    4.8
# ... with 140 more rows

您还可以跨多个列进行变异

mydf <- iris[-5]

mydf %>% group_by(n = (row_number() -1) %/% n1) %>%
  mutate(across(everything(), ~ mean(.), .names = "{.col}_mean"))

# A tibble: 150 x 9
# Groups:   n [22]
   Sepal.Length Sepal.Width Petal.Length Petal.Width     n Sepal.Length_me~ Sepal.Width_mean Petal.Length_me~
          <dbl>       <dbl>        <dbl>       <dbl> <dbl>            <dbl>            <dbl>            <dbl>
 1          5.1         3.5          1.4         0.2     0              4.9             3.39             1.44
 2          4.9         3            1.4         0.2     0              4.9             3.39             1.44
 3          4.7         3.2          1.3         0.2     0              4.9             3.39             1.44
 4          4.6         3.1          1.5         0.2     0              4.9             3.39             1.44
 5          5           3.6          1.4         0.2     0              4.9             3.39             1.44
 6          5.4         3.9          1.7         0.4     0              4.9             3.39             1.44
 7          4.6         3.4          1.4         0.3     0              4.9             3.39             1.44
 8          5           3.4          1.5         0.2     1              4.8             3.21             1.43
 9          4.4         2.9          1.4         0.2     1              4.8             3.21             1.43
10          4.9         3.1          1.5         0.1     1              4.8             3.21             1.43
# ... with 140 more rows, and 1 more variable: Petal.Width_mean <dbl>