I need to sum columns in a dataframe where the columns that need to be summed are defined in a separate data frame. Reproducible example below.
dataset <- tibble(L1 = runif(100, 0, 1),
L2 = runif(100, 0, 1),
L3 = runif(100, 0, 1),
L4 = runif(100, 0, 1))
cols_to_sum <- tibble(col1 = c("L1","L2"),
col2 = c("L3","L4"))
In the example above I need to create two additional columns in dataset, one called "L1L3" which is the sum of L1 and L3 and similar for L2 and L4. The desired output should look like the dataframe below. The cols_to_sum dataframe could have any number of rows and the dataset could have any number of columns.
dataset <- tibble(L1 = runif(100, 0, 1),
L2 = runif(100, 0, 1),
L3 = runif(100, 0, 1),
L4 = runif(100, 0, 1)) %>%
mutate(L1L3 = L1 + L3,
L2L4 = L2 + L4)