O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

Using summarize along with the plyr function

We calculate the number of rows and the mean mpg of the data frame grouped by cylinders. The following command shows the output when summarize is used:

> ddply(auto, .(cylinders), summarize, freq=length(cylinders), meanmpg=mean(mpg)) 
cylinders freq  meanmpg 
1      3cyl    4 20.55000 
2      4cyl  204 29.28676 
3      5cyl    3 27.36667 
4      6cyl   84 19.98571 
5      8cyl  103 14.96311  

The following command creates histograms of mpg for each cylinder:

> par(mfrow = c(1,2))> d_ply(auto,"cylinders",summarise,    hist(mpg,xlab="Miles per Gallon",main="Histogram of Miles per      Gallon",breaks=5))

The following plot is the output of preceding command:

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

Start Free Trial

No credit card required