[R] lapply across using multiple columns

Kingsford Jones kingsfordjones at gmail.com
Wed Jun 24 04:17:45 CEST 2009


try:

dfr <- data.frame(A=rnorm(50), B=runif(50),
    X=factor(sample(c('L1','L2'),50,repl=TRUE)))

by(dfr[, 1:2], dfr$X, mean)

hth,

Kingsford Jones


On Tue, Jun 23, 2009 at 7:57 PM, R_help Help<rhelpacc at gmail.com> wrote:
> Hi,
>
> To set a simple an clear picture of what I'd like to do, here is an example.
> I would like to calculate mean of column A and B bucketed by factor in
> column X in a data.frame. lapply or aggregate operates on a single column. I
> found dapply function in some package which doesn't seem to exist any more.
>
> Is there anyway that I can accomplish something like this? Thank you so much
> in advance.
>
> adschai
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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