【问题标题】:Get average of a number of rows in all of the columns in R [duplicate]获取R中所有列中行数的平均值[重复]
【发布时间】:2016-08-11 06:32:29
【问题描述】:

我有一个矩阵 A[400x100] 。我怎样才能得到每列中每 20 行的平均值。最后,我将所有值放入矩阵 B[20,100] 中。例如:

B[1,1] = mean(A[1:20])
B[2,1] = mean(A[21:40]) .....

【问题讨论】:

标签: r matrix average


【解决方案1】:

我们可以的

i1 <- as.numeric(gl(nrow(A), 20, nrow(A)))
B <- t(sapply(split(seq_len(nrow(A)), i1), function(i) colMeans(A[i,])))
dim(B) 
#[1]  20 100

数据

A <- matrix(1:40000, nrow = 400, ncol= 100)

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 2015-10-19
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多