[R] Any way to apply TWO functions with tapply()?
Gabor Grothendieck
ggrothendieck at gmail.com
Fri May 7 17:17:13 CEST 2010
Actually I have never mentioned the majority of the items in this post
in that form. remix only appeared yesterday on CRAN and as pointed
out to me offline the binaries are still not there (but should be
automatically built shortly).
On Fri, May 7, 2010 at 9:49 AM, David Winsemius <dwinsemius at comcast.net> wrote:
> That was a superb answer to a question that has already appeared in various
> forms on r-help at least 4 times that I can remember just this week. I think
> your text could be appended without much editing into the FAQ. It could
> then have its own hyperlink, and you wouldn't need to type it again the next
> umpteen times that it will re-appear.
>
> --
> David.
>
>
> On May 7, 2010, at 8:50 AM, Gabor Grothendieck 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.
>>>
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
More information about the R-help
mailing list