如何根据条件显示dataframe列的平均值
我有一个如下所示的数据框:如何根据条件显示dataframe列的平均值,r,dataframe,R,Dataframe,我有一个如下所示的数据框: > head(diabetes) tp gl bp t s bm p a class .row 1 6 148 72 35 0 33.6 0.627 50 0 1 2 1 85 66 29 0 26.6 0.351 31 1 2 3 8 183 64 0 0 23.3 0.672 32 0 3 4 1 89 66 23 94 28.1 0.167 21 1
> head(diabetes)
tp gl bp t s bm p a class .row
1 6 148 72 35 0 33.6 0.627 50 0 1
2 1 85 66 29 0 26.6 0.351 31 1 2
3 8 183 64 0 0 23.3 0.672 32 0 3
4 1 89 66 23 94 28.1 0.167 21 1 4
5 0 137 40 35 168 43.1 2.288 33 0 5
6 5 116 74 0 0 25.6 0.201 30 1 6
我知道我可以通过摘要(糖尿病)
查看所有列的平均值;但是,我希望看到与类
列相关的每个列的平均值。类列只能有两个值0
或1
。因此,我希望看到类为0
或1
时每列的平均值
例如:
> summary(diabetes$tp) #shows mean for tp column
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 1.000 3.000 3.845 6.000 17.000
> summary(diabetes$tp$class==0) #my effort to show mean for tp column when class == 0
试一试
或
要在最后一次努力中取得成功,需要:
summary(糖尿病$tp[tp$class==0])
library(dplyr)
diabetes %>%
group_by(class) %>%
summarise_each(funs(mean), -.row)
aggregate(.~class, diabetes[-10], mean)