I have a df with multiple columns like in the example bellow. I want to change all zeros by the number two in the columns from A1 to A5, but I do not want to write all columns names in the mutate function. Does anyone know how I can create a loop that goes from A1 to A5 and change the zeros by two with a mutate function?
df = data.frame(A1 = c(0,1,1,0,0,1,1,1), B1 = c(0,1,1,0,0,0,0,0), C1 = c(1,1,1,0,0,0,0,0), A2 = c(0,1,1,0,0,0,0,0), A3 = c(1,1,1,0,1,1,1,1), A4 = c(1,1,1,0,0,1,1,1), A5 = c(0,1,1,0,0,1,1,1), C2 = c(1,1,1,0,0,1,0,0))
I tried to do that with the following loop
for (i in 1:5) {
a = paste0('A', i)
df = df %>% mutate(a = ifelse( a == 0, 2, 1))
}
...but the mutate function does not acccept the variable.