[R] Any way to apply TWO functions with tapply()?

Fredrik Karlsson dargosch at gmail.com
Fri May 7 14:01:50 CEST 2010


Hi,

What you can do is define your own function which takes a vector of
values, computes the statistics you want and then returns a string
which displays the output the way you want it. Then use this function
in your tapply call.

like (untested)

mySummary <- function(x) {

  paste(mean(x),sd(x),sep=",")

}

tapply(data$response, list(data$targets, data$conditions) ,mySummary)

Of course, if you need a different output format, then you'll have to
adapt the paste call.

/Fredrik

On Fri, May 7, 2010 at 11:39 AM, Phil Wieland <phwiel at gmx.de> wrote:
>
> I need to compute the mean and the standard deviation of a data set and would
> like to have the results in one table/data frame. I call tapply() two times
> and do then merge the resulting tables to have them all in one table. Is
> there any way to tell tapply() to use the functions mean and sd within one
> function call? Something like tapply(data$response, list(data$targets,
> data$conditions), c(mean, sd)).
>
> Thanks in advance.
> --
> View this message in context: http://r.789695.n4.nabble.com/Any-way-to-apply-TWO-functions-with-tapply-tp2133924p2133924.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.
>



-- 
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."



More information about the R-help mailing list