[R] average of rows of each column

Rui Barradas ruipbarradas at sapo.pt
Fri Apr 4 21:23:57 CEST 2014


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.
>




More information about the R-help mailing list