One way using `Reduce`:
set.seed(45)
grp <- factor(rep(letters[1:10], each=10)) # equivalent of your column x
# dummy data
df <- as.data.frame(matrix(sample(1:1000, replace=T),
ncol=length(levels(grp))))
# solution
Reduce('+', split(df, grp))/length(levels(grp))
Arun
On Saturday, January 19, 2013 at 3:49 PM, ya wrote:
> Hi list,
>
> Thank you vey much for reading this post.
>
> I have a data frame, I am trying to split it into a couple of data frame
> using one of the columns, say, x. After I get the data frames, I am planning
> to treat them as matrices and trying to calculate an element by element mean
> matrix. Could anyone give me some advice how to do it?
>
> So far, I know that if I have a couple of matrices, say
> data1,data2,data3,data4...dataN, I can do it like this:
>
> data=array(cbind(data1,data2,data3,data4,....dataN), c(2, 3, N))
> #2 refers to row number of matrix, 3 refers to column number of matrix, N
> refers to number of matrices to be averaged.
> meanmtrx=apply(data,1:2,mean)
>
> but I do not know how to use the resulting data frames with cbind(). Maybe
> there are other better ways. Any advice is appreciated.
>
> Thank you very much.
>
> Have a nice day.
>
> ya
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] (mailto:[email protected]) 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]]
______________________________________________
[email protected] 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.