Hello Everyone- I'm calculating summary statistics on a dataset (~4000 records, observations are not uniformly distributed) using summaryBy and trying to add a column with the number of observations to the output as well. What occurs to me is to use nrow(), but this doesn't appear to be working I'm able to replicate the same results with an example from the summaryBy docs: data(dietox) dietox12<- subset(dietox,Time==12) library(doBy) #this one works summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=c(mean,var,length)) #adding nrow doesn't give the number of rows summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=c(mean,var,length,nrow)) There must be a way to do this, but I can't figure it out. I suspect there is another function that would be compatible with summaryBy. Thanks in advance. -Tony [[alternative HTML version deleted]]
On Mar 17, 2010, at 11:23 AM, Tony Laidig wrote:> Hello Everyone- > I'm calculating summary statistics on a dataset (~4000 records, > observations are not uniformly distributed) using summaryBy and trying > to add a column with the number of observations to the output as well. > What occurs to me is to use nrow(), but this doesn't appear to be > working > > I'm able to replicate the same results with an example from the > summaryBy docs: > > data(dietox) > dietox12<- subset(dietox,Time==12) > library(doBy) > #this one works > summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=c(mean,var,length)) > #adding nrow doesn't give the number of rows > summaryBy(Weight+Feed~Evit > +Cu,data=dietox12,FUN=c(mean,var,length,nrow)) >I'm a bit puzzled. One of my many newbie mistakes was to assume that length() applied to dataframes would tell me how many rows it had. It appears that the authors of summaryBy have figured out how to get length() to tell you the number of observations, presumably on a subsetted vector where length would make sense. So ... it's not clear why you also want nrow (which would not make sense for a subsetted vector).> > There must be a way to do this, but I can't figure it out. I suspect > there is another function that would be compatible with summaryBy. > > Thanks in advance. > -TonyDavid Winsemius, MD West Hartford, CT
Use NROW rather than nrow. On Wed, Mar 17, 2010 at 11:23 AM, Tony Laidig <c64 at mit.edu> wrote:> Hello Everyone- > I'm calculating summary statistics on a dataset (~4000 records, > observations are not uniformly distributed) using summaryBy and trying > to add a column with the number of observations to the output as well. > What occurs to me is to use nrow(), but this doesn't appear to be working > > I'm able to replicate the same results with an example from the > summaryBy docs: > > data(dietox) > dietox12<- subset(dietox,Time==12) > library(doBy) > #this one works > summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=c(mean,var,length)) > #adding nrow doesn't give the number of rows > summaryBy(Weight+Feed~Evit+Cu,data=dietox12,FUN=c(mean,var,length,nrow)) > > > There must be a way to do this, but I can't figure it out. I suspect > there is another function that would be compatible with summaryBy. > > Thanks in advance. > -Tony > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >