2

I have run into a recurring issue when using the car package recode function. If I recreate a publicly used example (http://susanejohnston.wordpress.com/2012/07/18/find-and-replace-in-r-part-1-recode-in-the-library-car/)

and do:

y <- sample(c("Perch", "Goby", "Trout", "Salmon"), size = 10, replace = T)  
y1 <- recode(y, "c("Perch", "Goby") = "Perciform" ; c("Trout", "Salmon") = "Salmonid"")

It returns:

Error: unexpected symbol in "y1 <- recode(y, "c("Perch"

I am running R 3.1.0 and using car_2.0-22

I assume that the author of the page was able to complete their action posted, but I can't recreate it and it is the same issue I have in my data. Thoughts?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
MattT
  • 99
  • 4

1 Answers1

3

I was the author of the wordpress document - code is wrong and thanks for flagging the issue.

Problem is that car::recode syntax requires a single quote rather than a double quote (or see comment from @MrFlick below on other acceptable syntax).

y1 <- recode(y, 'c("Perch", "Goby") = "Perciform" ; c("Trout", "Salmon") = "Salmonid"')
y1

[1] "Perciform" "Salmonid"  "Perciform" "Salmonid"  "Salmonid"  "Perciform" "Salmonid"  "Perciform"
[9] "Salmonid"  "Perciform"

Should work.

susjoh
  • 2,298
  • 5
  • 20
  • 20
  • 2
    It's not so much that it requires a single quote, you just need to have your quotes properly encoded. You could include double quotes by escaping them: `recode(y, "c(\"Perch\", \"Goby\") = \"Perciform\"")` but of course that's messy. Or you could wrap the singles and doubles as well. `recode(y, "c('Perch', 'Goby') = 'Perciform'")`. – MrFlick Dec 19 '14 at 00:38