Re: [R] Looking for a sort of tapply() to data frames

From: January Weiner <january_at_uni-muenster.de>
Date: Sat 17 Dec 2005 - 02:47:49 EST

Hi,

On 12/15/05, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> You don't get them as a column but you get them as the
> component labels.
>
> by(df, df$Day, function(x) colMeans(x[,-1]))
>
> If you convert it to a data frame you get them as the rownames:
>
> do.call("rbind", by(df, df$Day, function(x) colMeans(x[,-1])))

Thanks! that helps a lot. But I still run into problems with this. Sorry for bothering you with newbie questions, if my problems are trivial, point me to a suitable guide (I did read the introductory materials on R).

First: it works for colMeans, but it does not work for a function like this:

do.call("rbind", by(df, df$Day, function(x) cor(df$val1, df$val2))

it says "Error in do.call(....) : second argument must be a list". I do not understand this, as the second argument is "b" of the class "by", as it was in the case of colMeans, so it did not change...?

Second: in case of colMeans (where it works) it returns a matrix, and I have troubles getting it back to the data.frame, so I can access blah$Day. Instead, I have smth like that:

> do.call("rbind",b)

    V2 V3 V4 V5 V7
Tue 19 15 2 0 1.538462
Wed 5 3 6 1 1.285714

...and I do not know how to acces, for example, values for "Tue", except with [1,] -- which is somewhat problematic. For example, I would like to display the 3 days for which V7 is highest. How can I do that?

> I think you want class(df) which shows its a data frame.

Ops. Sorry, I didn't guess it from the manual :-)

> aggregate(df[,-1], df[,1,drop = FALSE], mean)

But why is df[,1,drop=FALSE] a list? I don't get it...

> aggregate(df[,-1], list(Day = df$Day), mean)

Yeah, I figured out that one.

> Another alternative is to use summaryBy from the doBy package found
> at http://genetics.agrsci.dk/~sorenh/misc/ :
>
> library(doBy)
> summaryBy(cbind(var1, var2) ~ Day, data = df)

I think I am not confident enough with the basic data types in R, I need to understand them before I go over to specialized packages :-)

Again, thanks a lot,
January

--
------------ January Weiner 3  ---------------------+---------------
Division of Bioinformatics, University of Muenster  |  Schloßplatz 4
(+49)(251)8321634                                   |  D48149 Münster
http://www.uni-muenster.de/Biologie.Botanik/ebb/    |  Germany

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Dec 17 02:58:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:39 EST