按 r 中多列分组的平均值

Mean by groups by multiple columns in r

我有下一个数据框

obs1   obs2   obs3   zone
1       0      1     Rural
1       1      1     Rural
0       1      1     Urban
1       0      0     Urban
0       1      0     Rural

我想得到这样的东西

Mean Rural    0.6666
Mean Urban    0.5

这可能吗?

你可以试试:

aggregate(values ~ zone, cbind(stack(df[-4]), df[4]), mean)

   zone    values
1 Rural 0.6666667
2 Urban 0.5000000

或者(假设没有缺失值):

tapply(rowMeans(df[-4]), df[4], mean)

    Rural     Urban 
0.6666667 0.5000000 

要添加 {dplyr} 方法:

library(dplyr)

dat %>% 
  group_by(zone) %>% 
  summarise(values = mean(c_across(obs1:obs3)))

#> # A tibble: 2 x 2
#>   zone  values
#>   <chr>  <dbl>
#> 1 Rural  0.667
#> 2 Urban  0.5


# data
dat <- tibble(obs1 = c(1,1,0,1,0),
       obs2 = c(0,1,1,0,1),
       obs3 = c(1,1,1,0,0),
       zone = c("Rural", "Rural", "Urban", "Urban", "Rural"))

reprex package (v2.0.1)

于 2022-05-24 创建