For a data frame, I would like to create a new column that is the sum of other columns with dplyr
's mutate()
. These columns to sum over should be dynamically definable.
For example, I would like to sum two specified columns from mtcars
:
library(dplyr)
columns_to_sum <- c("gear", "carb")
mtcars %>%
rowwise() %>%
mutate(newcol = sum(columns_to_sum))
but this results (expectedly) in error
invalid 'type' (character) of argument
How should I change the last line of code so that the sum over the requested columns is taken?