我们可以按“类型”分组并使用
summarise_all
. 假设我们要对
mean
和
sd
并获得摘要列
library(dplyr)
f %>%
group_by(type) %>%
summarise_all(funs(mean(.) + round(sd(.), 2), mean(.)- round(sd(.), 2)))
# A tibble: 2 x 7
# type `A_+` `B_+` `C_+` `A_-` `B_-` `C_-`
# <fctr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 a 34.07 27.69 17.41 19.93 9.31 14.59
#2 b 23.62 31.45 37.49 19.38 21.55 20.51
如果需要
character
班
f %>%
group_by(type) %>%
summarise_all(funs(paste(mean(.), round(sd(.), 2), sep=" ± ")))
# A tibble: 2 x 4
# type A B C
# <fctr> <chr> <chr> <chr>
#1 a 27 ± 7.07 18.5 ± 9.19 16 ± 1.41
#2 b 21.5 ± 2.12 26.5 ± 4.95 29 ± 8.49