zerfetzen
2008-Nov-03 15:03 UTC
[R] How do you apply a function to each variable in a data frame?
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) -- View this message in context: http://www.nabble.com/How-do-you-apply-a-function-to-each-variable-in-a-data-frame--tp20304332p20304332.html Sent from the R help mailing list archive at Nabble.com.
Jorge Ivan Velez
2008-Nov-03 17:02 UTC
[R] How do you apply a function to each variable in a data frame?
Dear zerftezen, Try this: # Data set.seed(123) X=as.data.frame(matrix(rnorm(100),ncol=10)) # Percentiles 10 and 90 using apply t(apply(X,2,quantile,probs=c(0.1,0.9))) # The same using sapply t(sapply(X,function(x) quantile(x,probs=c(0.1,0.9)))) HTH, Jorge On Mon, Nov 3, 2008 at 10:03 AM, zerfetzen <zerfetzen@yahoo.com> 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) > -- > View this message in context: > http://www.nabble.com/How-do-you-apply-a-function-to-each-variable-in-a-data-frame--tp20304332p20304332.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
hadley wickham
2008-Nov-03 17:08 UTC
[R] How do you apply a function to each variable in a data frame?
On Mon, Nov 3, 2008 at 9:02 AM, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:> Dear zerftezen, > Try this: > > # Data > set.seed(123) > X=as.data.frame(matrix(rnorm(100),ncol=10)) > > # Percentiles 10 and 90 using apply > t(apply(X,2,quantile,probs=c(0.1,0.9))) > > # The same using sapply > t(sapply(X,function(x) quantile(x,probs=c(0.1,0.9))))An alternative is the colwise function in the plyr package: library(plyr) colwise(quantile)(X, probs = c(0.1,0.9)) colwise(quantile)(mtcars, probs = c(0.1,0.9)) It always returns a data frame. You can also use catcolwise and numcolwise which only operate on categorical and numeric columns respectively. Hadley -- http://had.co.nz/
Carlos J. Gil Bellosta
2008-Nov-03 17:13 UTC
[R] How do you apply a function to each variable in a data frame?
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)
Erik Iverson
2008-Nov-03 18:24 UTC
[R] How do you apply a function to each variable in a data frame?
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)And how does that not work? For me, it does: test <- data.frame(a = rnorm(100), b = rnorm(100), c = rnorm(100)) sapply(test, quantile) You should say what you tried and most importantly, *how* you are not having luck, i.e., what errors or unexpected results you are getting. Using small examples like the 'test' data.frame can also help clear up misunderstandings.