0

非常基本的问题,但我无法通过搜索找到答案:

我正在尝试将序数变量的值重新编码为新值。

我尝试使用 car 包中的 recode() 函数,如下所示:

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

我收到以下错误消息:

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

'

谢谢你的帮助。

4

1 回答 1

3

在我看来它threecat是一个列表,而 car::recode 需要一个向量。里面有什么threecat?按照@mnel 的建议包含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'

如果 threecat 的元素是向量,则只需对向量元素运行 recode:

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

如果 threecat 是元素列表,则必须将其取消列出:

> 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

如果没有看到您实际使用的变量,很难说更多。

于 2012-11-28T03:09:06.183 回答