Dear useRs, I have a matrix of 120 row and 1000 columns.What I want is to get an average of a set of 12 rows starting from 1 till 120 for each column. Precisely, for column 1 the average of 1:10 rows, 11:20 rows.... 111:120. similarly for column 2, 3, 4.... 1000. So in the end i should have a matrix with 12 rows and 1000 columns. Thankyou very much in advance. Eliza [[alternative HTML version deleted]]
Hello,
Try the following.
m <- 120
n <- 10 # in your case this is 1000
mat <- matrix(rnorm(n*m), nrow = m)
fun <- function(x, na.rm = TRUE){
tapply(x, rep(1:12, each = 10), mean, na.rm = na.rm)
}
apply(mat, 2, fun)
apply(mat, 2, fun, na.rm = FALSE) # alternative
Hope this helps,
Rui Barradas
Em 04-04-2014 19:08, eliza botto escreveu:>
> Dear useRs,
> I have a matrix of 120 row and 1000 columns.What I want is to get an
average of a set of 12 rows starting from 1 till 120 for each column. Precisely,
for column 1 the average of 1:10 rows, 11:20 rows.... 111:120. similarly for
column 2, 3, 4.... 1000. So in the end i should have a matrix with 12 rows and
1000 columns.
> Thankyou very much in advance.
>
> Eliza
>
> [[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.
>
Something like (only 20 columns here): x <- matrix(rnorm(120*20), 120, 20) xagg <- aggregate(x, list(rep(1:12, each=10)), mean) ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 ----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of eliza botto Sent: Friday, April 4, 2014 1:08 PM To: r-help at r-project.org Subject: [R] average of rows of each column Dear useRs, I have a matrix of 120 row and 1000 columns.What I want is to get an average of a set of 12 rows starting from 1 till 120 for each column. Precisely, for column 1 the average of 1:10 rows, 11:20 rows.... 111:120. similarly for column 2, 3, 4.... 1000. So in the end i should have a matrix with 12 rows and 1000 columns. Thankyou very much in advance. Eliza [[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.