Skip to Content
The R Book
book

The R Book

by Michael J. Crawley
June 2007
Beginner to intermediate
950 pages
27h 8m
English
Wiley
Content preview from The R Book

Summarizing the Contents of Dataframes

There are three useful functions here

summary summarize all the contents of all the variables
aggregate create a table after the fashion of tapply
by perform functions for each level of specified factors

Use of summary and by with the worms database on p. 110.

The other useful function for summarizing a dataframe is aggregate. It is used like tapply (see p. 18) to apply a function (mean in this case) to the levels of a specified categorical variable (Vegetation in this case) for a specified range of variables (Area, Slope, Soil.pH and Worm.density are defined using their subscripts as a column index in worms[,c(2,3,5,7)]):

aggregate(worms[,c(2,3,5,7)],by=list(veg=Vegetation),mean)

          veg      Area     Slope   Soil.pH  Worm.density
1      Arable  3.866667  1.333333  4.833333      5.333333
2   Grassland  2.911111  3.666667  4.100000      2.444444
3      Meadow  3.466667  1.666667  4.933333      6.333333
4      rchard  1.900000  0.000000  5.700000      9.000000
5       Scrub  2.425000  7.000000  4.800000      5.250000

The by argument needs to be a list even if, as here, we have only one classifying factor. Here are the aggregated summaries for Vegetation and Damp:

aggregate(worms[,c(2,3,5,7)],by=list(veg=Vegetation,d=Damp),mean) veg d Area Slope Soil.pH Worm.density 1 Arable FALSE 3.866667 1.333333 4.833333 5.333333 2 Grassland FALSE 3.087500 3.625000 3.987500 1.875000 3 Orchard FALSE 1.900000 0.000000 5.700000 9.000000 4 Scrub FALSE 3.350000 5.000000 4.700000 7.000000 5 Grassland TRUE 1.500000 4.000000 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book, 3rd Edition

The R Book, 3rd Edition

Elinor Jones, Simon Harden, Michael J. Crawley
The Book of R

The Book of R

Tilman M. Davies
Learning R

Learning R

Richard Cotton

Publisher Resources

ISBN: 9780470510247Purchase book