R 101: Summarizing Data

March 25, 2014
By

(This article was first published on Mathew Analytics » R, and kindly contributed to R-bloggers)

When working with large amounts of data that is structured in a tabular format, a common operation is to summarize that data in different ways using specific variables. In Microsoft Excel, pivot tables are a nice feature that is used for this purpose. While not as “efficient” in relation to Excel pivot tables, R also has similar calculations that can be used to summarize large amount of data. In the following R code, I utilize R to summarize a data frame by specific variables.

## CREATE DATA
 
dat = data.frame(
  name=c("Tony","James","Sara","Alice","David","Angie","Don","Faith","Becky","Jenny",
         "Kristi","Neil","Brandon","Kara","Kendra","Liz","Gina","Amber","Alice","George"),
  state=c("KS","IA","CA","FL","MI","CO","KA","CO","KS","CA","MN","FL","NM","MS","GA",
          "IA","IL","ID","NY","NJ"),
  gender=c("M","M","F","F","F","M","F","M","F","F","F","M","M","F","F","F","F","F","F","M"),
  marital_status=c("M","S","S","S","M","M","S","M","S","M","M","S","S","S","M","M","S","M","S","M"),
  credit=c("good","good","poor","fair","poor","fair","fair","fair","good","fair",
           "good","good","poor","fair","poor","fair","fair","fair","good","fair"),
  owns_home=c(0,1,0,0,1,0,1,1,1,1,0,1,0,0,1,0,1,1,1,1),
  cost=c(500,200,300,150,200,300,400,450,250,150,500,200,300,150,200,300,400,450,250,150))
## AGGREGATE FUNCTION FROM BASE R
aggregate(cost ~ marital_status, data=dat, FUN=mean)
aggregate(cost ~ marital_status + gender, data=dat, FUN=mean)
aggregate(cost ~ marital_status + credit + gender, data=dat, FUN=mean)
 
## SUMMARY BY IN DOBY:
library(doBy)
summaryBy(cost ~ marital_status, data=dat, FUN=c(mean, sd))
summaryBy(cost ~ gender, data=dat, FUN=c(mean, sd))
summaryBy(cost ~ credit, data=dat, FUN=c(mean, sd))
 
## DDPLY IN PLYR
library(plyr)
ddply(dat, .(credit), "nrow")
ddply(dat, .(credit, gender), "nrow")
ddply(dat, .(marital_status), summarise, avg=mean(cost))
ddply(dat, .(marital_status, gender), summarise, avg=mean(cost))
ddply(dat, .(marital_status, gender, credit), summarise, avg=mean(cost))
 
## DPLYR PACKAGE
library(dplyr)
Good = filter(dat, credit=="good")
Good
arrange(Good, desc(cost))
select(Good, owns_home, cost)
mutate(Good, New_Value=cost/5)
by.type <- group_by(Good, gender)
summarise(by.type, num.types = n(), counts = sum(cost))
 
## SQLDF PACKAGE
library(sqldf)
sqldf("SELECT gender, COUNT(*) FROM dat GROUP BY gender")
sqldf("SELECT gender, credit, COUNT(*) FROM dat GROUP BY gender, credit")
sqldf("SELECT gender, credit, COUNT(*), AVG(cost) FROM dat GROUP BY gender, credit")

 


To leave a comment for the author, please follow the link and comment on his blog: Mathew Analytics » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.