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

January Weiner january at uni-muenster.de
Fri Dec 16 16:47:49 CET 2005


On 12/15/05, Gabor Grothendieck <ggrothendieck at 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 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

More information about the R-help mailing list