Use library(plyr) and library (dplyr)
library(plyr)
library(dplyr)
1. filter based on your need
data(mtcars)
names(mtcars)
pivot_filter = filter(mtcars, gear %in% c(3,4,5))
2. set up data frame for by using ‘group_by’
pivot_group = group_by(pivot_filter, cyl, vs)
3: calculate the summary statistics you want, including min, max, mean, etc.
Note: It is dplyr::summarise which return full results. If you only have one row of summarise result, add ‘dplyr::’ in front of function sunnarise
summarise(pivot_group,mean(disp), max(hp), min(qsec))
## # A tibble: 5 x 5
## # Groups: cyl [3]
## cyl vs `mean(disp)` `max(hp)` `min(qsec)`
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 4 0 120. 91 16.7
## 2 4 1 104. 113 16.9
## 3 6 0 155 175 15.5
## 4 6 1 205. 123 18.3
## 5 8 0 353. 335 14.5