Search code examples
rsyntax-errorgsubrecode

Odd gsub error for a recode function in R when I didn't use the gsub function


I regularly use the recode() function in R from the car library. It works great. However, now I'm getting an odd gsub error message and I've got no clue why since I haven't called the gsub() function in my commands.

I used the gsub() function the other day to pull commas out numeric values, but that was something completely different. Since getting the error, I specified the car::recode() function and restarted R & reloaded only the car library, but still I get the same error.

The code below is just a simple recode exercise for my students and now I'm boggled. Any thoughts?

# enter grade data
> trust <- c("D","C","B","D","E","C","A","F","D","C")

# recode letters to numbers
> library(car)
> trust.r <- recode(trust(" 'A'=5; 'B'=4; 'C'=3; 'D'=2; 'E'=1; 'G'=0 "))
Error in gsub("\n|\t", " ", recodes) : argument "recodes" is missing, with no default

# Weird error.  Specify recode() from car library
> trust.r <- car::recode(trust(" 'A'=5; 'B'=4; 'C'=3; 'D'=2; 'E'=1; 'G'=0 "))
Error in gsub("\n|\t", " ", recodes) : argument "recodes" is missing, with no default

# Still weird error.  Flip " and ' symbols, just in case
> trust.r <- car::recode(trust(' "A"=5; "B"=4; "C"=3; "D"=2; "E"=1; "G"=0 '))
Error in gsub("\n|\t", " ", recodes) : argument "recodes" is missing, with no default

Solution

  • I think Gregor's comment is correct. If you read the ?recode page, you'll see

    Usage
    recode(var, recodes, as.factor, as.numeric=TRUE, levels)

    Arguments
    var numeric vector, character vector, or factor.

    recodes character string of recode specifications: see below.

    So, don't try to embed the recodes inside var