Re: [R] best way to apply a list of functions to a dataset ?

2010-07-21 Thread Hadley Wickham
> ddply(ma, .(variable), summarise, mean = mean(value), sd = sd(value), >       skewness = skewness(value), median = median(value), >       mean.gt.med = mean.gt.med(value)) In principle, you should be able to do: ddply(ma, .(variable), colwise(each(mean, sd, skewness, median, mean.gt.med))) but

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-21 Thread Peter Ehlers
Dennis' ddply solution would be my choice. Here is a small variation that makes it easy to modify what list of functions is applied: # ma<- melt(attitude) f <- function(x,v) summarise(x, mean = mean(v), sd = sd(v), skewness = skewness(v), mean.gt.med = mean.gt.med(v) ) d

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-20 Thread Dennis Murphy
Hi: On Tue, Jul 20, 2010 at 5:37 PM, Glen Barnett wrote: > Hi Dennis, > > Thanks for the reply. > > Yes, that's easier, but the conversion to a matrix with rbind has > converted the output of that final function to a numeric. > If you look at the output of lapply(attitude, f), you'll see that t

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-20 Thread David Winsemius
On Jul 20, 2010, at 8:37 PM, Glen Barnett wrote: Hi Dennis, Thanks for the reply. Yes, that's easier, but the conversion to a matrix with rbind has converted the output of that final function to a numeric. I included that last function in the example secifically to preclude people assuming t

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-20 Thread Glen Barnett
Hi Dennis, Thanks for the reply. Yes, that's easier, but the conversion to a matrix with rbind has converted the output of that final function to a numeric. I included that last function in the example secifically to preclude people assuming that functions would always return the same type. I g

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-20 Thread Dennis Murphy
Hi: This might be a little easier (?): library(datasets) skewness <- function(x) mean(scale(x)^3) mean.gt.med <- function(x) mean(x)>median(x) # -- # construct the function to apply to each variable in the data frame f <- function(x) c(mean = mean(x), sd = sd(x), skewness = skewness(x),

Re: [R] best way to apply a list of functions to a dataset ?

2010-07-19 Thread Glen Barnett
Erk. Sorry about the wrapping issue on the comments in the code, which will interfere with a straight copypaste. Glen __ 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

[R] best way to apply a list of functions to a dataset ?

2010-07-19 Thread Glen Barnett
Assuming I have a matrix of data (or under some restrictions that will become obvious, possibly a data frame), I want to be able to apply a list of functions (initially producing a single number from a vector) to the data and produce a data frame (for compact output) with column 1 being the functio