[英]R Sum columns by index
I need to find a way to sum columns by their index,I'm working on a big read.csv
file, I'll show here a sample of the problem; 我需要找到一种方法来按列索引对列进行求和,我正在研究一个很大的read.csv
文件,我将在这里展示一个问题的样本; I'd like for example to sum from the 2nd to the 5th and from the 6th to the 7h the following matrix: 我想例如从第2到第5和从第6到第7h总和以下矩阵:
a 1 3 3 4 5 6
b 2 1 4 3 4 1
c 1 3 2 1 1 5
d 2 2 4 3 1 3
The result has to be like this: 结果必须是这样的:
a 11 11
b 10 5
c 7 6
d 8 4
The columns have all different names 列具有所有不同的名称
We can use rowSums
on the subset of columns ie 2:5 and 6:7 separately and then create a new data.frame
with the output. 我们可以在列的子集上使用rowSums
,即分别为2:5和6:7,然后使用输出创建一个新的data.frame
。
data.frame(df1[1], Sum1=rowSums(df1[2:5]), Sum2=rowSums(df1[6:7]))
# id Sum1 Sum2
#1 a 11 11
#2 b 10 5
#3 c 7 6
#4 d 11 4
The package dplyr has a function exactly made for that purpose: 包dplyr具有完全为此目的而制作的功能:
require(dplyr)
df1 = data.frame(a=c(1,2,3,4,3,3),b=c(1,2,3,2,1,2),c=c(1,2,3,21,2,3))
df2 = df1 %>% transmute(sum1 = a+b , sum2 = b+c)
df2 = df1 %>% transmute(sum1 = .[[1]]+.[[2]], sum2 = .[[2]]+.[[3]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.