I am trying to loop through data frames and recode all values of a specific named variable to NA for all data frames. This should return the separate data frames with the specified variable's values coded NA.
I tried using within() for this but I receive an error:
Error in UseMethod("within") : no applicable method for 'within' applied to an object of class "character"
A minimal working example is found below:
# Create df
a <- data.frame(seq(1:10), rep("x"))
b <- data.frame(seq(1:10), rep("y"))
# Rename vars for easy reference
colnames(a) <- c("num", "string")
colnames(b) <- c("num", "string")
# Create vector of variables names
vars <- c("a", "b")
# Loop through data frames and replace values of "string" with NA\
for (i in vars){
i <- within(i, {string <- NA})
}
Note: this is most relevant to Error in UseMethod("meta", x) : no applicable method for 'try-error' applied to an object of class "character" which remains largely unanswered.