0

I have a dataset that goes like this:

             var1 var2 var3 var4 var5 var6 varTotal

Person A:       5    5    5    5    5    5
        
Person B:       1    2    1    4    7    1

Person C:       0    1    3    2    3    2

For now, varTotal is empty, but I want to fill it up with summation of var1 through var4 (no var5 or var6).

Parfait
  • 104,375
  • 17
  • 94
  • 125

1 Answers1

0

We can use dplyr with rowSums(across(<tidy-select>)), se we can use the tidyselect part to select the columuns to feed to rowSums:

library(dplyr)

mydf %>% mutate(varTotal = rowSums(across(var1:var4)))
GuedesBF
  • 8,409
  • 5
  • 19
  • 37