Dennis & Hadley,

This does exactly what I need — thank you so much!

Regards,
Tom


On 10/4/11 5:34 PM, Dennis Murphy wrote:
Hi Hadley:

When I tried your function on the example data, I got the following:

dd<- data.frame(year = rep(2000:2008, each = 500), y = rnorm(4500))
g<- function(df, qs = c(.05, .25, .50, .75, .95)) {
  data.frame(q = qs, quantile(d$y, qs))
}
ddply(dd, .(year), g)

ddply(dd, .(year), g)
    year    q quantile.d.y..qs.
1  2000 0.05                NA
2  2000 0.25                NA
3  2000 0.50                NA
...
43 2008 0.50                NA
44 2008 0.75                NA
45 2008 0.95                NA
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
<repeated once per year>

This, however, does work (with a likely fix to the variable name afterwards):

g<- function(df, qs = c(.05, .25, .50, .75, .95)) {
  data.frame(q = qs, quantile(d[, 2], qs))
}

ddply(dd, .(year), g)
    year    q quantile.d...2...qs.
1  2000 0.05          -1.36670724
2  2000 0.25          -0.97786897
3  2000 0.50          -0.05982217
4  2000 0.75           0.33576399
5  2000 0.95           1.30389105
...

Dennis

On Tue, Oct 4, 2011 at 12:10 PM, Hadley Wickham<had...@rice.edu>  wrote:
# Function to compute quantiles and return a data frame
g<- function(d) {
   qq<- as.data.frame(as.list(quantile(d$y, c(.05, .25, .50, .75, .95))))
   names(qq)<- paste('Q', c(5, 25, 50, 75, 95), sep = '')
   qq   }
You could cut out the melt step by making this return a data frame:

g<- function(df, qs = c(.05, .25, .50, .75, .95)) {
  data.frame(q = qs, quantile(d$y, qs))
}

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



--
Thomas E Adams
National Weather Service
Ohio River Forecast Center
1901 South State Route 134
Wilmington, OH 45177

EMAIL:  thomas.ad...@noaa.gov

VOICE:  937-383-0528
FAX:    937-383-0033

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to