I've got a data frame having numerical data by zip code: ZIP DATA 94111 12135.545 93105 321354.65654 94111 545.555 94706 558858.66 ... ... I'm using this function to group records by ZIP and calculate the median of DATA: aggregate(d$DATA, list(Zip = d$ZIP), FUN=median, na.rm=T) but what I really want to do is to calculate several statistics (median, mean, etc.) for each group of records, so that I'll get a result organized like: Zip median(DATA) mean(DATA) 94706 565 555 94111 59585 66666 93105 595685 5555666 ... I tried using FUN=funstofun(median,mean), but found that doesn't work in aggregate. Is there a straightforward way to do what I'm after (I'm pretty new to R)? thanks _________________________________________________________________ Change the world with e-mail. Join the i?m Initiative from Microsoft. ld
Will something like this work for you:> d <- read.table(textConnection("ZIP DATA+ 94111 12135.545 + 93105 321354.65654 + 94111 545.555 + 94706 558858.66"), header=TRUE)> closeAllConnections() > aggregate(d$DATA, list(Zip = d$ZIP), FUN=median, na.rm=T)Zip x 1 93105 321354.66 2 94111 6340.55 3 94706 558858.66> do.call(rbind, by(d$DATA, list(Zip=d$ZIP), function(x){+ c(Mean=mean(x), Median=median(x)) + })) Mean Median 93105 321354.66 321354.66 94111 6340.55 6340.55 94706 558858.66 558858.66> > t(sapply(split(d$DATA, d$ZIP), function(x) c(Mean=mean(x),Median=median(x)))) Mean Median 93105 321354.66 321354.66 94111 6340.55 6340.55 94706 558858.66 558858.66 On Fri, May 16, 2008 at 10:18 AM, Mike ! <mc510@hotmail.com> wrote:> > > I've got a data frame having numerical data by zip code: > > ZIP DATA > 94111 12135.545 > 93105 321354.65654 > 94111 545.555 > 94706 558858.66 > ... ... > > I'm using this function to group records by ZIP and calculate the median of > DATA: > > aggregate(d$DATA, list(Zip = d$ZIP), FUN=median, na.rm=T) > > but what I really want to do is to calculate several statistics (median, > mean, etc.) for each group of records, so that I'll get a result organized > like: > > Zip median(DATA) mean(DATA) > 94706 565 555 > 94111 59585 66666 > 93105 595685 5555666 > ... > > I tried using FUN=funstofun(median,mean), but found that doesn't work in > aggregate. Is there a straightforward way to do what I'm after (I'm pretty > new to R)? thanks > _________________________________________________________________ > Change the world with e-mail. Join the i'm Initiative from Microsoft. > > ld > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
You might want to look at the summaryBy function in the doBy package summaryBy(varname ~ zip, data=DATA,FUN=c(mean,median) David Freedman Mike ! wrote:> > > > I've got a data frame having numerical data by zip code: > > ZIP DATA > 94111 12135.545 > 93105 321354.65654 > 94111 545.555 > 94706 558858.66 > ... ... > > I'm using this function to group records by ZIP and calculate the median > of DATA: > > aggregate(d$DATA, list(Zip = d$ZIP), FUN=median, na.rm=T) > > but what I really want to do is to calculate several statistics (median, > mean, etc.) for each group of records, so that I'll get a result organized > like: > > Zip median(DATA) mean(DATA) > 94706 565 555 > 94111 59585 66666 > 93105 595685 5555666 > ... > > I tried using FUN=funstofun(median,mean), but found that doesn't work in > aggregate. Is there a straightforward way to do what I'm after (I'm > pretty new to R)? thanks > _________________________________________________________________ > Change the world with e-mail. Join the i?m Initiative from Microsoft. > > ld > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/aggregate%28%29%2C-with-multiple-functions-in-FUN--tp17275892p17297580.html Sent from the R help mailing list archive at Nabble.com.
Dear R-users, I use Sweave for quite a long time but I still wonder what is the difference between snw and rnw files. Why these two file extensions ? I searched the web without success. Thanks for your answers. Delphine Fontaine