[英]How to sum a column in a matrix and paste the value in the last cell in r?
Is there a way to sum up all the values in from a column and paste them into the last "cell"?有没有办法总结一列中的所有值并将它们粘贴到最后一个“单元格”中?
Example: I have a matrix like:示例:我有一个矩阵,如:
1 2 3
1 0 0 1
2 0 0 2
3 0 0 6
and I want (need) a Matrix like:我想要(需要)一个像这样的矩阵:
1 2 3
1 0 0 0
2 0 0 0
3 0 0 9
In addition to that I need an iterative Approach, because my matrix dimensions vary.除此之外,我需要一种迭代方法,因为我的矩阵尺寸各不相同。
Thank you in advance!先感谢您!
edit:编辑:
the following solution worked:以下解决方案有效:
creating a second zero'd matrix with the same dimensions and the following code
matrix2[nrow(matrix2),] <- colSums(m1)
We provide several solutions depending on the interpretation of the question.我们根据问题的解释提供几种解决方案。 They all assume that a zero matrix is to be returned except for the last row.
他们都假设除了最后一行之外要返回一个零矩阵。 (1) sums just the last column, (2) sums column(s) i and (2a) sums all columns.
(1) 仅对最后一列求和,(2) 对 i 列求和,(2a) 对所有列求和。
All solutions are one line except for defining nr and/or nc and preserve the original m matrix returning the new matrix.除了定义 nr 和/或 nc 并保留返回新矩阵的原始 m 矩阵外,所有解决方案都是一行。
1) Using m
shown reproducibly in the note at the end multiply m by zero and then replace the last cell (which would be at position prod(dim(m))
, with the sum of the last column. 1)使用末尾注释中可重复显示的
m
将 m 乘以零,然后用最后一列的总和替换最后一个单元格(位于 position prod(dim(m))
处。
nc <- ncol(m)
replace(0 * is.na(m), prod(dim(m)), sum(m[, nc]))
## 1 2 3
## 1 0 0 0
## 2 0 0 0
## 3 0 0 9
If m
is known not have NAs then is.na(m) can be replaced with m in the above code.如果已知
m
没有 NA,则可以在上述代码中将 is.na(m) 替换为 m。
2) If the column is not necessarily last then if i is the index of the column to sum (or it can be a vector of the indexes of those columns to be summed should there be more than one) then: 2)如果列不一定是最后一列,那么如果 i 是要求和的列的索引(或者如果有多个列,它可以是要求和的那些列的索引的向量),那么:
i <- 3
nr <- nrow(m)
replace(0 * is.na(m), i * nr, colSums(m[, i, drop = FALSE]))
2a) If all columns are to be summed then i <- 1:nc so the above becomes: 2a)如果要对所有列求和,则 i <- 1:nc 因此上述变为:
nc <- ncol(m)
nr <- nrow(m)
replace(0 * is.na(m), (1:nc) * nr, colSums(m, na.rm = TRUE))
If you need to be able to handle matrices with zero columns then use seq_len(nc)
instead of 1:nc
.如果您需要能够处理具有零列的矩阵,请使用
seq_len(nc)
而不是1:nc
。
m <- matrix(c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 6L), 3, dimnames = list(1:3, 1:3))
Suppose your matrix is given by假设您的矩阵由下式给出
mat <- matrix(c(0,0,1,0,0,2,0,0,6,0,0,7), nrow=4, byrow = TRUE)
Then然后
mat_2 <- matrix(0L, nrow=dim(mat)[1], ncol=dim(mat)[2])
gives you matrix with same dimensions and containing just 0s
.为您提供具有相同尺寸且仅包含
0s
的矩阵。 Now calculate the sum by现在计算总和
mat_2[dim(mat)[1],] <- apply(mat, 2, sum)
Another option is colSums
另一种选择是
colSums
m1[nrow(m1),] <- colSums(m1, na.rm = TRUE)
m1[-nrow(m1),] <- 0
m1
# 1 2 3
#1 0 0 0
#2 0 0 0
#3 0 0 9
Or with addmargins
或使用
addmargins
addmargins(m1, 1)[-1,] * rep(0:1, c(nrow(m1)- 1, 1))
m1 <- matrix(c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 6L), 3, dimnames = list(1:3, 1:3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.