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.
Look at the reshape package - it allows that when using "cast" ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Fri, May 7, 2010 at 12:39 PM, Phil Wieland <phwiel@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@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]]
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."
Gabor Grothendieck
2010-May-07 12:50 UTC
[R] Any way to apply TWO functions with tapply()?
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. >
Gabor Grothendieck
2010-May-07 15:20 UTC
[R] Any way to apply TWO functions with tapply()?
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. >> >