I have a data frame like mtcars
, and a string vector of column names such as c("mpg", "cyl", "disp", "hp", "drat")
, and I would like to sum together all of the columns into a new one.
i would normally use something like
mtcars %>% transmute(new_col = mpg + cyl + disp + hp + drat)
new_col
1 300.90
2 300.90
3 231.65
4 398.48
5 564.85
6 356.86
7 630.51
However, this becomes very tedious when you have 100s of column names, stored in a vector.
So my question is, is there a way of summing together lots of columns, where the column names are held in a vector of strings?