I am currently working with a dataframe called df consisting of about 100 variables. Among these is a group of factor variables named quite similar: A_1, A_2, A_3, A_4 ... etc. I want to do some calculations on this subset of variables - to begin with, I want to add the same new factor level to all of them.
levels(df$A_1) <- c(levels(df$A_1), "x")
levels(df$A_2) <- c(levels(df$A_2), "x")
levels(df$A_3) <- c(levels(df$A_3), "x")
levels(df$A_4) <- c(levels(df$A_4), "x")
levels(df$A_5) <- c(levels(df$A_5), "x")
levels(df$A_6) <- c(levels(df$A_6), "x")
...
This code works pretty well. However, I was wondering wether there isn't a method to access all of these variables at the same time as they all share the same prefix.