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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.