i have matrix na , want find mean of every 3 columns each row.
set.seed(100) <- matrix(sample(c(na, 1:4), 90, replace = true), 10)
if use
t(apply(a, 1, tapply, gl(3, 3), mean))
the code cannot find average colums including na. wonder how can modify code. lot.
we can converting 3d array , rowmeans
after looping through 3rd margin.
res <- apply(array(a, c(nrow(a), 3, 3)), 3, rowmeans, na.rm = true) all.equal(res, t(apply(a, 1, tapply, gl(3, 3), mean, na.rm = true)), check.attributes = false) #[1] true
Comments
Post a Comment