[R] aggregate with non-scalar functions
carslaw
david.carslaw at kcl.ac.uk
Wed Aug 4 18:25:02 CEST 2010
Hi R-users,
Since R.2.11 aggregate can now deal with non-scalar functions, which is very
useful to me.
However, I have a question about how best to process the output.
test <- data.frame(a = rep(c("g1", "g2"), each = 50), b = runif(100))
res <- aggregate(test$b, list(group = test$a), function(x) quantile(x, probs
= c(0.05, 0.95)))
> res
group x.5% x.95%
1 g1 0.00899229 0.91327509
2 g2 0.01863110 0.86187829
Looks like exactly what I want, but:
dim(res)
[1] 2 2
> res[, 2]
5% 95%
[1,] 0.00899229 0.91327509
[2,] 0.01863110 0.86187829
> sapply(res, class)
group x
"factor" "matrix"
I first thought the output contained 3 columns, but it does not (and I
*think*
the help says as much).
So if I want 3 columns and all outputs accessible, I do:
res <- data.frame(subset(res, select = -x), res$x)
> dim(res)
[1] 2 3
And that works fine.
Is this the best way of obtaining a data frame with the 3 columns?
I know in this example I could have used tapply, but the actual data
consists of several variables.
Thanks.
David
--
View this message in context: http://r.789695.n4.nabble.com/aggregate-with-non-scalar-functions-tp2313759p2313759.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list