使用列名R [duplicate]在同一行中的列之间求和

时间:2021-08-01 16:53:51

This question already has an answer here:

这个问题在这里已有答案:

I have a dataset similar to what is below:

我有一个类似于下面的数据集:

ID | Var1 | Var2 | Var3 | Sum
-----------------------------
1     3      4      7      
2     1      2      3     
3     3      5      16    

I need to create the Sum variable above. I need to be able to sum different values in a series of variables. For above, it'd be 14, 6, and 24, for rows 1, 2, and 3. I want to do it with something like

我需要在上面创建Sum变量。我需要能够在一系列变量中求和不同的值。对于上面,对于行1,2和3,它是14,6和24.我想用类似的东西来做

df$Sum <- sum('Var1', 'Var2', 'Var3')

I can't seem to find an example this simple. Everything I have found involved code that I either do not understand, or it's able to deal with situations far more complex than what I've got, and will have for the foreseeable future. I realize it would probably behoove me to learn something more in depth, but at this point I would like it to be simple. I have tried sum, aggregate, and rowSum, and none of them have gotten me what I want.

我似乎无法找到一个这么简单的例子。我发现的所有内容都涉及到我不理解的代码,或者它能够处理比我所拥有的更复杂的情况,并且在可预见的未来将会有这样的情况。我意识到我可能应该更深入地学习一些东西,但在这一点上我希望它很简单。我试过了sum,aggregate和rowSum,但没有一个能让我得到我想要的东西。

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


2  

Try:

df$Sum <- (df$Var1 + df$Var2 + df$Var3)

#1


2  

Try:

df$Sum <- (df$Var1 + df$Var2 + df$Var3)