Muenchen, Robert A (Bob)
2006-Dec-28 14:47 UTC
[R] Aggregation using list with Hmisc summarize function
Hi All, I'm using the Hmisc summarize function and used list instead of llist to provide the by variables. It generated an error message. Is this a bug, or do I misunderstand how Hmisc works with lists? The program below demonstrates the error message. Thanks, Bob x<-1:8 group <- c(1,1,1,1,2,2,2,2) gender<- c(1,2,1,2,1,2,1,2) mydata<-data.frame(x,group,gender) attach(mydata) # Creating a list using Hmisc llist works: summarize(x, by=llist(group,gender), FUN=mean, na.rm=TRUE) # Creating a list using built-in list function does not: summarize(x, by= list(group,gender), FUN=mean, na.rm=TRUE) ========================================================Bob Muenchen (pronounced Min'-chen), Manager Statistical Consulting Center U of TN Office of Information Technology 200 Stokely Management Center, Knoxville, TN 37996-0520 Voice: (865) 974-5230 FAX: (865) 974-4810 Email: muenchen@utk.edu Web: http://oit.utk.edu/scc <http://oit.utk.edu/scc> , News: http://listserv.utk.edu/archives/statnews.html <http://listserv.utk.edu/archives/statnews.html> ======================================================== [[alternative HTML version deleted]]
Frank E Harrell Jr
2006-Dec-28 16:28 UTC
[R] Aggregation using list with Hmisc summarize function
Muenchen, Robert A (Bob) wrote:> Hi All, > > > > I'm using the Hmisc summarize function and used list instead of llist to > provide the by variables. It generated an error message. Is this a bug, > or do I misunderstand how Hmisc works with lists? The program below > demonstrates the error message. > > > > Thanks, > > Bob > > > > x<-1:8 > > group <- c(1,1,1,1,2,2,2,2) > > gender<- c(1,2,1,2,1,2,1,2) > > > > mydata<-data.frame(x,group,gender) > > attach(mydata) > > > > # Creating a list using Hmisc llist works: > > summarize(x, by=llist(group,gender), FUN=mean, na.rm=TRUE) > > > > # Creating a list using built-in list function does not: > > summarize(x, by= list(group,gender), FUN=mean, na.rm=TRUE)Use llist so summarize will know how to label the output. Also your attach( ) is not functional as your variables are already in the search path. Whenever you do need to attach, consider the use of with( ) instead. Frank> > > > ========================================================> Bob Muenchen (pronounced Min'-chen), Manager > Statistical Consulting Center > U of TN Office of Information Technology-- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Gabor Grothendieck
2006-Dec-28 16:37 UTC
[R] Aggregation using list with Hmisc summarize function
Note that you don't really need list or llist in this case: summarize(x, by = mydata[2:3], FUN = mean, na.rm = TRUE) Also you could use aggregate: aggregate(mydata[1], mydata[2:3], mean, na.rm = TRUE) On 12/28/06, Muenchen, Robert A (Bob) <muenchen at utk.edu> wrote:> Hi All, > > > > I'm using the Hmisc summarize function and used list instead of llist to > provide the by variables. It generated an error message. Is this a bug, > or do I misunderstand how Hmisc works with lists? The program below > demonstrates the error message. > > > > Thanks, > > Bob > > > > x<-1:8 > > group <- c(1,1,1,1,2,2,2,2) > > gender<- c(1,2,1,2,1,2,1,2) > > > > mydata<-data.frame(x,group,gender) > > attach(mydata) > > > > # Creating a list using Hmisc llist works: > > summarize(x, by=llist(group,gender), FUN=mean, na.rm=TRUE) > > > > # Creating a list using built-in list function does not: > > summarize(x, by= list(group,gender), FUN=mean, na.rm=TRUE) > > > > ========================================================> Bob Muenchen (pronounced Min'-chen), Manager > Statistical Consulting Center > U of TN Office of Information Technology > 200 Stokely Management Center, Knoxville, TN 37996-0520 > Voice: (865) 974-5230 > FAX: (865) 974-4810 > Email: muenchen at utk.edu > Web: http://oit.utk.edu/scc <http://oit.utk.edu/scc> , > News: http://listserv.utk.edu/archives/statnews.html > <http://listserv.utk.edu/archives/statnews.html> > ========================================================> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >