R语言 按组计算多列的总体平均值

gmxoilav  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(195)

我有几个列有每个参与者的React时间(RT),这是我的数据框的简化版本(实际上我有20个RT列):

df <- data.frame(id=c(1,1,1,5,5,5), RT_0=c(0.467,NA,0.634,0.416,NA,0.35), RT_1=c(0.3,0.35,0.301,0.267,0.3,0.449), RT_2=c(1.251,NA, 0.334,0.34, 0.317,0.42), RT_3=c(0.284,0.316,0.283,0.367,0.55,0.35),pos=c(1,6,2,4,2,6))

id  RT_0    RT_1    RT_2    RT_3    pos
 1  0.467   0.3     1.251   0.284   1
 1  NA      0.35    NA      0.316   6
 1  0.634   0.301   0.334   0.283   2
 5  0.416   0.267   0.34    0.367   4
 5  NA      0.3     0.317   0.55    2
 5  0.35    0.449   0.42    0.35    6

我想通过id计算RT列的总体平均值。例如,1号参与者所有React时间的平均值为0.452。
大概是这样的

id  RT_0    RT_1    RT_2    RT_3    pos  mean
 1  0.467   0.3     1.251   0.284   1    0.452
 1  NA      0.35    NA      0.316   6    0.452
 1  0.634   0.301   0.334   0.283   2    0.452
 5  0.416   0.267   0.34    0.367   4    0.375
 5  NA      0.3     0.317   0.55    2    0.375
 5  0.35    0.449   0.42    0.35    6    0.375
9gm1akwq

9gm1akwq1#

我们可以按"id"分组,在across(或pick)、unlist中选择列名为vectorstarts_with "RT_"的列,并使用mean获得总体平均值,将其指定为mutate中的新列

library(dplyr)
df %>% 
  group_by(id) %>% 
  mutate(mean = mean(unlist(across(starts_with("RT_"))),
        na.rm = TRUE)) %>%
  ungroup
  • 输出
# A tibble: 6 × 7
     id   RT_0  RT_1   RT_2  RT_3   pos  mean
  <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>
1     1  0.467 0.3    1.25  0.284     1 0.452
2     1 NA     0.35  NA     0.316     6 0.452
3     1  0.634 0.301  0.334 0.283     2 0.452
4     5  0.416 0.267  0.34  0.367     4 0.375
5     5 NA     0.3    0.317 0.55      2 0.375
6     5  0.35  0.449  0.42  0.35      6 0.375

或使用data.table

library(data.table)
setDT(df)[, mean := mean(unlist(.SD), na.rm = TRUE), 
  .(id), .SDcols = patterns("^RT_")]
  • 输出
> df
   id  RT_0  RT_1  RT_2  RT_3 pos      mean
1:  1 0.467 0.300 1.251 0.284   1 0.4520000
2:  1    NA 0.350    NA 0.316   6 0.4520000
3:  1 0.634 0.301 0.334 0.283   2 0.4520000
4:  5 0.416 0.267 0.340 0.367   4 0.3750909
5:  5    NA 0.300 0.317 0.550   2 0.3750909
6:  5 0.350 0.449 0.420 0.350   6 0.3750909

相关问题