[R] How do you apply a function to each variable in a data frame?

Carlos J. Gil Bellosta cgb at datanalytics.com
Mon Nov 3 18:13:21 CET 2008


Data frames are lists themselves.

Something like

do.call( rbind, lapply( my.data.frame, quantile, probs=c(0.1,0.9)) )

should work.

Carlos J. Gil Bellosta
http://www.datanalytics.com


On Mon, 2008-11-03 at 07:03 -0800, zerfetzen wrote:
> I want to apply a more complicated function than what I use in my example,
> but the idea is the same:
> 
> Suppose you have a data frame named x and you want to a function applied to
> each variable, we'll just use the quantile function for this example.  I'm
> trying all sorts of apply functions, but not having luck.  My best guess
> would be:
> 
> sapply(x, FUN=quantile)



More information about the R-help mailing list