0

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

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.

Any help is appreciated.

Adam_S
  • 687
  • 2
  • 12
  • 24
  • 1
    `df$Sum <- rowSums(df[2:4])` – Jaap Mar 16 '18 at 18:13
  • @Jaap please provide link to the duplicate question. I looked here first, and didn't find anything this simple and easy to use. Everything was more complex, and that's not what I was looking for. – Adam_S Mar 16 '18 at 18:38

1 Answers1

2

Try:

df$Sum <- (df$Var1 + df$Var2 + df$Var3)
AnnVyrgyl
  • 56
  • 6