簡體   English   中英

R 無法識別我的因子變量的級別以允許重新編碼/組合

[英]R is not recognizing the levels of my factor variable to allow for recoding/combining

我有一個數據框 (MyData1),其中因子變量分為幾個級別:同意、強烈同意、不同意等。我試圖組合級別以使其成為二進制(例如,如果同意/不同意等,則為是)。

我一直在沿着這些路線得到一些東西:

combineLevels(MyData1$V45,levs=c("Disagree"),newLabel="False")

>Error: requested levels: " Disagree " are not in the legal list of factor levels:"  Agree  Disagree  Neutral  Strongly agree 

我在重新編碼功能上遇到了同樣的問題。 有沒有辦法弄清楚為什么 R 不識別能夠產生函數的級別?

您沒有指明函數combineLevels來源(至少有兩個版本)。 在基礎 R 中很容易做到:

set.seed(42)
V45 <- sample(c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree"), 100, replace=TRUE)
V45 <- factor(V45, levels=c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree"))
table(V45)
# V45
#    Strongly Agree             Agree          Disagree Strongly Disagree 
#                28                30                16                26 

只需更改級別:

V45binary <- V45 # So we can recover the original if something goes wrong
levels(V45binary) <- c("Yes", "Yes", "False", "False")
table(V45binary)
# V45binary
#   Yes False 
#    58    42 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM