I am trying to replace values with NA based on a condition. I have written the following code which works fine:
df <- df %>%
mutate(var1 = ifelse(years > 0, NA, var1)
I need run this code for 80+ different variables, so I am trying to use a for loop (instead of copy-pasting). I have tried the following code, but it doesn't work
for(i in c(13:100))
{df <- df %>%
mutate(i = ifelse(df$years > 0, NA, i))}
Ideally, the output of the loop will be stored back in the original dataframe. Thank you so much, any help is greatly appreciated!