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]]
If I understand, you can try this:
DF <- data.frame(X = rep(1:3, each = 4), A = rnorm(12), B = rnorm(12))
aggregate(DF[,c("A", "B")], list(DF$X), mean)
On Tue, Jun 23, 2009 at 10:57 PM, R_help Help <rhelpacc@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@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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
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.
>
Possibly Parallel Threads
- aggregating irregular time series
- Smart way to sum a column in a matrix across all members in a list
- POSIX time conversion doesn't display digit
- Smart way to check if a column with certain name exists in a data.frame
- Defining class as a member of another class