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

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Mon, 03 Nov 2008 12:02:20 -0500

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_at_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_at_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]]



R-help_at_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. Received on Mon 03 Nov 2008 - 17:04:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Nov 2008 - 17:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive