[R] Summarize by two or more attributes

Felipe Carrillo mazatlanmexico at yahoo.com
Tue May 17 20:49:42 CEST 2011


Like This?

x<-rep(c(1,15),10)
y<-rnorm(20)
z<-c(rep("auto",10),rep("bus",10))
a<-rep(c(1,1,2,2,3,3,4,4,5,5),2)
#Create Data frame
Df<-data.frame(Source=x,Rate=y,Bin=a,Type=z)
Df

ddply(Df,c('Type','Bin'),summarise,Summed=sum(Rate))

 # Adding a column to Df
ddply(Df,c('Type','Bin'),mutate,Summed=sum(Rate))
  
# Convert the result to a list
dlply(Df,c('Type','Bin'),summarise,Summed=sum(Rate))


 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
http://www.fws.gov/redbluff/rbdd_jsmp.aspx




----- Original Message ----
> From: LCOG1 <jroll at lcog.org>
> To: r-help at r-project.org
> Sent: Tue, May 17, 2011 9:48:36 AM
> Subject: [R] Summarize by two or more attributes
> 
> Okay everyone heres a likely softball for someone.
> 
> Consider the following data frame:
> 
> #Create data
> x<-rep(c(1,15),10)
> y<-rnorm(20)
> z<-c(rep("auto",10),rep("bus",10))
> a<-rep(c(1,1,2,2,3,3,4,4,5,5),2)
> #Create Data frame
> Df<-data.frame(Source=x,Rate=y,Bin=a,Type=z)
> 
> 
> I want to create a new column the equals the sum of the Rates for each type
> (1,15) by Bin.  
> 
> A related question:  I have been using R for a while now and usually
> manipulate my data in data frames but i know lists are better for R so
> perhaps the above should be done using lists.  Feel free to offer
> suggestions coming from that angle.  
> 
> Thanks guys
> 
> JR-
> 
> 
> 
> --
> View this message in context: 
>http://r.789695.n4.nabble.com/Summarize-by-two-or-more-attributes-tp3529825p3529825.html
>
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list