3

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.

Community
  • 1
  • 1
user3614648
  • 1,685
  • 1
  • 16
  • 39
  • 1
    `"a"` and `"b"` are a character strings. You should put `a` and `b` in a list and iterate through the list. That's the recommended R way. – Rich Scriven Jun 17 '16 at 19:59
  • See the [following post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) on how to put data.frames in a list. Also take a look at gregor's answer there that gives great tips on how to work with lists of data.frames. – lmo Jun 17 '16 at 20:02

0 Answers0