[英]Best way to convert numeric variable to ordered factor
I have a data frame that looks something like this: 我有一个看起来像这样的数据框:
df1 <- data.frame(V1=rnorm(n = 100, mean=0, sd=1),
Edu=sample(x = c(-999,12,13,14,16,1), size = 100,
replace = T, prob = c(0.05,0.2,.2,0.2,0.2,0.15)))
I want to convert the variable Edu
to an ordered factor variable. 我想将变量Edu
转换为有序因子变量。 I can convert it to a character variable with this code: 我可以使用以下代码将其转换为字符变量:
lutedu <- c('-999' = NA, '12' = "High School", '13' = "Associate's",
'14' = "Associate's", '16' = "Bachelor's",
'18' = "Master's, Graduate/professional", '21' = "PhD")
df1$Edu <- lutedu[as.character(df1$Edu)]
and from there I could convert the character variable to an ordered factor with ordered()
: 然后从那里我可以使用ordered()
将字符变量转换为有序因子:
df1$Edu <-
ordered(
x = df1$Edu, levels = c(
"High School", "Associate's", "Bachelor's",
"Master's, Graduate/professional", "PhD"
)
)
Is there a better way of doing this? 有更好的方法吗?
Instead of recoding with a named vector and then calling ordered
, you can save yourself a step by calling ordered
and using both the levels
and the labels
arguments: 无需使用命名的矢量进行编码,然后调用ordered
,可以通过调用ordered
并同时使用levels
和labels
参数来节省步骤:
ordered(edu, levels=c(-999, 12, 13, 14, 16, 1),
labels=c("NA", "High School", "Associate's", "Bachelor's",
"Master's/Graduate", "PhD"))
# [1] High School Master's/Graduate Master's/Graduate Bachelor's Associate's
# [6] Master's/Graduate High School Master's/Graduate High School PhD
# ...
Data : 资料 :
set.seed(144)
edu <- sample(x = c(-999,12,13,14,16,1), size = 100,
replace = T, prob = c(0.05,0.2,.2,0.2,0.2,0.15))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.