I need to programmatically add multiple variables whose name is in a vector. For example, given this vector:
myvars <- c("Expectation", "Interesting", "Useful", "OralPresentation")
how can I write the following expression using the previous vector?
df %>%
  mutate(TotalEvaluation = Expectation + Interesting + Useful + OralPresentation)
We can use rowSums after subsetting the columns of the dataset
library(dplyr)
df %>% 
      mutate(TotalEvaluation = rowSums(.[myvars]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With