## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# How to do it...

To use the split-apply-combine strategy for data analysis with plyr, follow these steps:

1. Calculate mean mpg for each cylinder type (two versions):
```> ddply(auto, "cylinders", function(df) mean(df\$mpg))
> ddply(auto, ~ cylinders, function(df) mean(df\$mpg))
cylinders       V1
1      3cyl 20.55000
2      4cyl 29.28676
3      5cyl 27.36667
4      6cyl 19.98571
5      8cyl 14.96311 ```
1. Calculate the mean, minimum, and maximum mpg for each cylinder type and model year:
`> ddply(auto, c("cylinders","model_year"), function(df) c(mean=mean(df\$mpg), min=min(df\$mpg), max=max(df\$mpg))) > ddply(auto, ~ cylinders + model_year, function(df) c(mean=mean(df\$mpg), min=min(df\$mpg), max=max(df\$mpg))) cylinders model_year mean min max 1 3cyl 72 19.00000 19.0 19.0 2 3cyl 73 18.00000 ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required