【发布时间】:2020-01-11 17:30:05
【问题描述】:
我在“分解”字符变量和标记/重新编码不同级别时遇到了一些麻烦。使用tidyverse 或Base R 如recode 或fct_collapse 等是否有有效的方法(即更少的编码)...谢谢
#This is what I have (a character variable)
x <- c("No", "Yes", "No2", "No3", "Maybe", "undecided",
"probably", "dont know", NA)
x
#I want a factor with three ordered levels as follows:
#where No = c("No", "No2", "No3")
#Yes = c("Yes")
#other = c("Maybe", "undecided", "probably")
#NA = c("dont know", NA)
# and the levels would be 0 = "No", 1 = "Yes" and 2 = "Maybe"
#that is:
#xfact
# [1] No Yes other
# Levels: No Yes other
#
# as.integer(xfact)
# [1] 0, 1, 2```
【问题讨论】:
标签: r tidyverse recode forcats