mfrumin
2008-Jun-17 14:40 UTC
[R] re sultant column names from reshape::cast, with a fun.aggregate vector
try this: scores.melt = data.frame(grade = floor(runif(100, 1,10)), variable 'score', value = rnorm(100)); cast(scores.melt, grade ~ variable, fun.aggregate = c(mean, length)) it has the nice column names of: grade score_mean score_length 1 1 0.08788535 8 2 2 0.16720313 15 3 3 0.41046299 7 4 4 0.13928356 13 ... but now try this: cast(scores.melt, grade ~ variable, fun.aggregate = c(mean, function(x) sum(x < 0))) and you get a huge mess: grade score_mean score_function.x..sum.x...0. 1 1 0.08788535 4 2 2 0.16720313 6 3 3 0.41046299 2 4 4 0.13928356 5 I would think that something like this would fix it up, but no dice: cast(scores.melt, grade ~ variable, fun.aggregate = c(mean, num.neg function(x) sum(x < 0))) that is, why not look at names(fun.aggregate)? or am I missing something? thanks, Mike I would think that -- View this message in context: http://www.nabble.com/resultant-column-names-from-reshape%3A%3Acast%2C-with-a-fun.aggregate-vector-tp17910885p17910885.html Sent from the R help mailing list archive at Nabble.com.
hadley wickham
2008-Jun-17 14:49 UTC
[R] re sultant column names from reshape::cast, with a fun.aggregate vector
> I would think that something like this would fix it up, but no dice: > > cast(scores.melt, grade ~ variable, fun.aggregate = c(mean, num.neg > function(x) sum(x < 0))) > > that is, why not look at names(fun.aggregate)? or am I missing something?Yes, that's a bug in each (the function which turns a vector of functions into a function that returns a named vector of outputs). I've added a note to my to do. In the meantime, you can do: num.neg <- function(x) sum(x < 0) cast(scores.melt, grade ~ variable, fun.aggregate = c(mean, num.neg)) Hadley -- http://had.co.nz/