0
votes

Very basic question, but I wasn't able to find an answer by searching:

I'm trying to recode the values of an ordinal variable to new values.

I tried using the recode() function from the car package like this:

recode(x, "0=1; 1=2; 3=2")

and I get the following error message:

Error in recode(threecat, "0=1; 1=2; 3=2") : 
  (list) object cannot be coerced to type 'double

'

Thanks for your help.

1
Please make your example reproducible. Include the output of dput(head(x)). It appears it is an issue with your data (given the erorr message) - mnel
@user1541682 At this stage you should normally do one of three things - either (i) respond to the requests for more details; (ii) ask further questions to resolve your outstanding problem; or (iii) if the answer was acceptable, you should so mark it - Glen_b

1 Answers

3
votes

It looks to me like threecat is a list, and car::recode expects a vector. What's in threecat? Follow @mnel's suggestion to include the result of dput(head(threecat)).

> x<-c(0,1,2,3,4)
> recode(x, "0=1; 1=2; 3=2")
[1] 1 2 2 2 4
> y<-list(x)
> y
[[1]]
[1] 0 1 2 3 4

> recode(y, "0=1; 1=2; 3=2")
Error in recode(y, "0=1; 1=2; 3=2") : 
  (list) object cannot be coerced to type 'double'

If threecat has an element that's a vector, you can just run recode on the vector element:

> recode(y[[1]], "0=1; 1=2; 3=2")
[1] 1 2 2 2 4

If threecat is a list of elements, you'll have to unlist it:

> yy <- list(0,1,2,3,4)
> yy
[[1]]
[1] 0

[[2]]
[1] 1

[[3]]
[1] 2

[[4]]
[1] 3

[[5]]
[1] 4

> recode(unlist(yy), "0=1; 1=2; 3=2")
[1] 1 2 2 2 4

It's difficult to say more without seeing the variable you're actually using.