求累积和,然后取R中的值的平均值
我想计算第一个求累积和,然后取R中的值的平均值,r,matrix,cumsum,R,Matrix,Cumsum,我想计算第一个(n-1)列(如果我们有n列矩阵)的累积和,然后对值进行平均。我创建了一个示例矩阵来完成此任务。我有下面的矩阵 ma = matrix(c(1:10), nrow = 2, ncol = 5) ma [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 我想找到以下内容 ans = matrix(c(1,2,2,3,3,4,4,5), nrow = 2,
(n-1)列
(如果我们有n
列矩阵)的累积和,然后对值进行平均。我创建了一个示例矩阵来完成此任务。我有下面的矩阵
ma = matrix(c(1:10), nrow = 2, ncol = 5)
ma
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
我想找到以下内容
ans = matrix(c(1,2,2,3,3,4,4,5), nrow = 2, ncol = 4)
ans
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 3 4 5
以下是我的r
功能
ColCumSumsAve <- function(y){
for(i in seq_len(dim(y)[2]-1)) {
y[,i] <- cumsum(y[,i])/i
}
}
ColCumSumsAve(ma)
ColCumSumsAve我是这样做的
> t(apply(ma, 1, function(x) cumsum(x) / 1:length(x)))[,-NCOL(ma)]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 3 4 5
这将cumsum
函数按行应用于矩阵ma
,然后除以正确的长度以获得平均值(cumsum(x)
和1:length(x)
将具有相同的长度)。然后简单地用t
进行转置,并用[,-NCOL(ma)]
删除最后一列
函数没有输出的原因是您没有返回任何内容。您应该以return(y)
结束函数,或者按照马吕斯的建议简单地以y
结束函数。不管怎样,您的函数似乎并没有给您正确的响应。下面是我如何做的
> t(apply(ma, 1, function(x) cumsum(x) / 1:length(x)))[,-NCOL(ma)]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 3 4 5
k <- t(apply(ma,1,cumsum))[,-ncol(k)]
for (i in 1:ncol(k)){
k[,i] <- k[,i]/i
}
k
这将cumsum
函数按行应用于矩阵ma
,然后除以正确的长度以获得平均值(cumsum(x)
和1:length(x)
将具有相同的长度)。然后简单地用t
进行转置,并用[,-NCOL(ma)]
删除最后一列
函数没有输出的原因是您没有返回任何内容。您应该以return(y)
结束函数,或者按照马吕斯的建议简单地以y
结束函数。无论如何,您的函数似乎并没有给您正确的响应。kk有几个错误
k <- t(apply(ma,1,cumsum))[,-ncol(k)]
for (i in 1:ncol(k)){
k[,i] <- k[,i]/i
}
k
解决方案
这就是我所测试的和有效的:
colCumSumAve <- function(m) {
csum <- t(apply(X=m, MARGIN=1, FUN=cumsum))
res <- t(Reduce(`/`, list(t(csum), 1:ncol(m))))
res[, 1:(ncol(m)-1)]
}
这是正确的
说明:
colCumSumAve <- function(m) {
csum <- t(apply(X=m, MARGIN=1, FUN=cumsum)) # calculate row-wise colsum
res <- t(Reduce(`/`, list(t(csum), 1:ncol(m))))
# This is the trickiest part.
# Because `csum` is a matrix, the matrix will be treated like a vector
# when `Reduce`-ing using `/` with a vector `1:ncol(m)`.
# To get quasi-row-wise treatment, I change orientation
# of the matrix by `t()`.
# However, the output, the output will be in this transformed
# orientation as a consequence. So I re-transform by applying `t()`
# on the entire result at the end - to get again the original
# input matrix orientation.
# `Reduce` using `/` here by sequencial list of the `t(csum)` and
# `1:ncol(m)` finally, has as effect `/`-ing `csum` values by their
# corresponding column position.
res[, 1:(ncol(m)-1)] # removes last column for the answer.
# this, of course could be done right at the beginning,
# saving calculation of values in the last column,
# but this calculation actually is not the speed-limiting or speed-down-slowing step
# of these calculations (since this is sth vectorized)
# rather the `apply` and `Reduce` will be rather speed-limiting.
}
然而,这使:
> ColCumSumsAve(ma)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1.5 1.666667 1.75 9
[2,] 3 3.5 3.666667 3.75 10
问题在于,矩阵中的cumsum
是按列方向而不是按行计算的,因为它将矩阵视为向量(沿列穿过矩阵)
已更正的原始功能
经过一番摸索,我意识到正确的解决方案是:
ColCumSumsAve <- function(y){
res <- matrix(NA, nrow(y), ncol(y)-1)
# create empty matrix with the dimensions of y minus last column
for (i in 1:(nrow(y))) { # go through rows
for (j in 1:(ncol(y)-1)) { # go through columns
res[i, j] <- sum(y[i, 1:j])/j # for each position do this
}
}
res # return `res`ult by calling it at the end!
}
注:dim(y)[2]
是ncol(y)
-和dim(y)[1]
是nrow(y)
-
相反,seq_len()
,1:
更短,我想更快一点
注意:我首先给出的解决方案会更快,因为它使用apply
、向量化cumsum
和Reduce
-<代码>for
-R中的循环速度较慢
后期说明:不太确定第一个解决方案是否更快。自R-3.x以来,似乎for
循环的速度更快<代码>减少将是限速功能,有时速度会非常慢。有几个错误
解决方案
这就是我所测试的和有效的:
colCumSumAve <- function(m) {
csum <- t(apply(X=m, MARGIN=1, FUN=cumsum))
res <- t(Reduce(`/`, list(t(csum), 1:ncol(m))))
res[, 1:(ncol(m)-1)]
}
这是正确的
说明:
colCumSumAve <- function(m) {
csum <- t(apply(X=m, MARGIN=1, FUN=cumsum)) # calculate row-wise colsum
res <- t(Reduce(`/`, list(t(csum), 1:ncol(m))))
# This is the trickiest part.
# Because `csum` is a matrix, the matrix will be treated like a vector
# when `Reduce`-ing using `/` with a vector `1:ncol(m)`.
# To get quasi-row-wise treatment, I change orientation
# of the matrix by `t()`.
# However, the output, the output will be in this transformed
# orientation as a consequence. So I re-transform by applying `t()`
# on the entire result at the end - to get again the original
# input matrix orientation.
# `Reduce` using `/` here by sequencial list of the `t(csum)` and
# `1:ncol(m)` finally, has as effect `/`-ing `csum` values by their
# corresponding column position.
res[, 1:(ncol(m)-1)] # removes last column for the answer.
# this, of course could be done right at the beginning,
# saving calculation of values in the last column,
# but this calculation actually is not the speed-limiting or speed-down-slowing step
# of these calculations (since this is sth vectorized)
# rather the `apply` and `Reduce` will be rather speed-limiting.
}
然而,这使:
> ColCumSumsAve(ma)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1.5 1.666667 1.75 9
[2,] 3 3.5 3.666667 3.75 10
问题在于,矩阵中的cumsum
是按列方向而不是按行计算的,因为它将矩阵视为向量(沿列穿过矩阵)
已更正的原始功能
经过一番摸索,我意识到正确的解决方案是:
ColCumSumsAve <- function(y){
res <- matrix(NA, nrow(y), ncol(y)-1)
# create empty matrix with the dimensions of y minus last column
for (i in 1:(nrow(y))) { # go through rows
for (j in 1:(ncol(y)-1)) { # go through columns
res[i, j] <- sum(y[i, 1:j])/j # for each position do this
}
}
res # return `res`ult by calling it at the end!
}
注:dim(y)[2]
是ncol(y)
-和dim(y)[1]
是nrow(y)
-
相反,seq_len()
,1:
更短,我想更快一点
注意:我首先给出的解决方案会更快,因为它使用apply
、向量化cumsum
和Reduce
-<代码>for
-R中的循环速度较慢
后期说明:不太确定第一个解决方案是否更快。自R-3.x以来,似乎for
循环的速度更快Reduce将是限速功能,有时速度会非常慢。您所需要的就是rowMeans
:
nc <- 4
cbind(ma[,1],sapply(2:nc,function(x) rowMeans(ma[,1:x])))
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 3 4 5
nc您只需要rowMeans
:
nc <- 4
cbind(ma[,1],sapply(2:nc,function(x) rowMeans(ma[,1:x])))
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 3 4 5
nc如果您只是在for循环下面添加一个带有y
的行,但仍然在ColCumSumsAve
函数中,您将得到输出,因为您将显式返回修改后的矩阵。但是,输出与您期望的结果不匹配,而且我在理解它们应该如何计算方面有点困难。@Marius,例如,在结果矩阵中,第一列与第一列的输入矩阵相同。但是第2列,[1,2]
元素是{1+3)/2=2
,[2,2]元素是(2+4)/2=3',[1,3]
是(1+3+5)/3=3
等等。如果你只是在for循环下面添加一行y
,但仍然在ColCumSumsAve
函数中,你会得到输出,因为你会显式返回修改后的矩阵。但是输出与你想要的结果不匹配,而且我在理解它们应该如何计算时有点困难例如,@Marius,在结果矩阵中,第一列与第一列的输入矩阵相同。但是第二列的[1,2]
元素是{1+3)/2=2
,[2,2]元素是(2+4)/2=3',[1,3]
元素是(1+3+5)/3
,依此类推。