[R] Question about ggplot2 and stat_smooth

Dennis Murphy djmuser at gmail.com
Tue Oct 4 23:34:28 CEST 2011


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 <hadley at 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/
>



More information about the R-help mailing list