Recode in Car package returns unexpected symbol when recoding strings

927 views Asked by At

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?

1

There are 1 answers

1
susjoh On BEST ANSWER

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.