Reputation: 9636
I have a data frame that looks like this:
> 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
I know I can see the means for all columns with summary(diabetes)
; however, I would like to see the means for each column as it relates to the class
column. The class column can have only two values 0
or 1
. So I would like to see the mean for each column when the class is 0
or 1
.
For example:
> 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
Upvotes: 2
Views: 66
Reputation: 887118
Try
library(dplyr)
diabetes %>%
group_by(class) %>%
summarise_each(funs(mean), -.row)
Or
aggregate(.~class, diabetes[-10], mean)
Upvotes: 3