为每行求和某些列的单元格 [英] sum cells of certain columns for each row
本文介绍了为每行求和某些列的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算某些列的总和,然后对每一行应用此总和.不幸的是,我只能走到第一步.我现在如何为每一行实现它?我知道 R 不需要循环;什么是好的方法?
I would like to calculate sums for certain columns and then apply this summation for every row. Unfortunately, I can only get to the first step. How do I now make it happen for each row? I know that R doesn't need loops; what are good approaches?
我的矩阵 (zscore) 如下所示:
My matrix (zscore) looks like this:
a b c t y
1 3 4 7 7 4
2 4 56 6 6 4
3 3 3 2 1 7
4 3 88 9 9 9
现在我想根据某些列计算每行的行总和.对于一行,它可能如下所示:
Now I would want to calculate the row sum for each row, based on some of the columns. For one row it could look like this:
f1 <- sum(zscore[1,1:2], zscore[1,3], zscore[1,5])
我现在如何为每一行执行此操作?
How do I do that now for each row?
推荐答案
你可以这样做:
summed <- rowSums(zscore[, c(1, 2, 3, 5)])
这篇关于为每行求和某些列的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文