Suppose I am working with the mtcars dataset, and I would like to add:
1 to all values in the column: mpg
2 to all values in the column: cyl
3 to all values in the column: disp
I would like to keep all columns in mtcars, and refer to the columns by their names rather than their index.
Here's my current attempt:
library("tidyverse")
library("rlang")
data(mtcars)
mtcars_colnames <- quo(c("mpg", "cyl", "disp"))
num <- c(1, 2, 3)
mtcars %>% mutate(across(!!! mtcars_colnames, function(x) {x + num[col(.)]}))
I'm stuck on how to dynamically add (1,2,3) to columns (mpg, cyl, disp). Thanks in advance.