I would like to use the column name from a vector in mutate to get output like you would from the following
df <- mtcars %>%
select(gear, carb, vs, am, cyl) %>%
mutate(Name1 = ifelse(gear == carb, 1, 0)) %>%
mutate(Name2 = ifelse(gear == vs, 1, 0))
However, the real dataframe I will be using will be of a different size each time so I can't do it like that. I have tried something like this or get() but mutate won't accept either. Ignore the fact the columns will be all the same, the key thing is that I want to have the column name selected from the vector. Is it possible? Thank you!
names <- c("name1", "name2", "name3")
for (i in 1:length(names)) {
df <- df %>%
mutate(paste0(names[i]) = ifelse(gear == carb, 1, 0))
}