0

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
Shawn Janzen
  • 369
  • 3
  • 15
  • 2
    `recode` uses `gsub` internally. When you do `recode(trust(...))` you are using `trust` like it's a function, but above you define it as an object (because you have a parenthesis after it). I think the correct syntax is something like `recode(trust, recodes = "'A'=5; 'B'=4;...")` – Gregor Thomas Nov 16 '18 at 14:08
  • Wow....now I'm embarrassed. Total rookie mistake. :) Thanks! Guess I was staring at my screen too long. – Shawn Janzen Nov 16 '18 at 19:07

1 Answers1

1

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

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Thanks for the supporting comment Carl! Total mistake on my end, but I probably started over-thinking the issue when I saw that error message and got confused. – Shawn Janzen Nov 16 '18 at 19:09