0

I have a dataset where I need to calculate a sum-variable of multiple variables. I created a for-loop to perform this one variable at a time:

for (i in 1:nrow(df)) {
  df$Q1[i] = sum(df$Q1_A[i], df$Q1_B[i], df$Q1_C[i], na.rm = T)
}

Is there a way to do this for all variables (Q1:Q60) at once? Basically I need a function that adjusts the variable-number from 1 to 2 to 3 to ... to 60. Do I need a for-loop within a for-loop?

Thanks in advance!

Naren Murali
  • 19,250
  • 3
  • 27
  • 54
JefVDA
  • 1
  • https://dplyr.tidyverse.org/articles/rowwise.html – Phil Aug 22 '22 at 14:48
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 22 '22 at 15:54

0 Answers0