[英]recode numeric variable
非常基本的問題,但是我無法通過搜索找到答案:
我正在嘗試將序數變量的值重新編碼為新值。
我嘗試過使用汽車包裝中的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
'
謝謝你的幫助。
在我看來, threecat
是一個列表,而car :: recode需要一個向量。 threecat
什么? 遵循dput(head(threecat))
的建議,以包含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的元素是矢量,則可以在vector元素上運行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
如果不查看實際使用的變量,很難說更多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.