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

Gabor Grothendieck ggrothendieck at gmail.com
Fri May 7 17:20:12 CEST 2010


As pointed out to me offline, data.table should be added to the list
of relevant packages as well.  Its primary advantage is for large data
sets as it is very fast.  Its interface does take some getting used to
but its most recent version on CRAN does have several vignettes which
should ease learning.

On Fri, May 7, 2010 at 8:50 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> tapply does handle functions with vector outputs, e.g. using the built
> in CO2 data set the following data frame is returned:
>
>> f <- function(x) data.frame(mean = mean(x), sd = sd(x))
>> do.call(rbind, tapply(CO2$uptake, CO2$Type, f))
>            mean   sd
> Quebec      33.5 9.67
> Mississippi 20.9 7.82
>
> Note that if you replace data.frame with c in f then you get a matrix
> out instead of a data.frame.
>
> There is also somewhat similar functionality in summaryBy (doBy
> package), summary.formula (Hmisc package), ddply (plyr package), remix
> (remix package), melt and cast (reshape package) and sqldf (sqldf
> package).
>
> Of these summaryBy in the doBy package is particularly easy to specify
> and produces a data frame:
>
> library(doBy)
> summaryBy(uptake ~ Type, data = CO2, FUN = c(mean, sd))
>
> remix and summary.formula in Hmisc have particularly attractive output
> but do not produce data frames.  Hmisc even has a plot method.  The
> specification to remix is also simple here and, in fact, is identical
> to the summaryBy line above except it uses lower case fun.  sqldf uses
> SQL for the specification which may be an advantage if you know SQL
> better than R.
>
> On Fri, May 7, 2010 at 5: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.
>>
>



More information about the R-help mailing list