我有一个数值变量(QS),范围从1-10。我想创建一个分类变量
差:QS< 5,和 好:QS> 5
所以我现在有两个分类变量......好与坏
在R中执行此操作的最佳方法是什么?
答案 0 :(得分:10)
我会在此推荐cut
或factor
+ levels
。一个小例子:
set.seed(1)
QS <- sample(10, 15, replace = TRUE)
QS
# [1] 3 4 6 10 3 9 10 7 7 1 3 2 7 4 8
cut(QS, c(0, 5, 10), labels=c("Bad", "Good"))
# [1] Bad Bad Good Good Bad Good Good Good Good Bad Bad Bad Good Bad Good
# Levels: Bad Good
X <- factor(QS)
levels(X) <- list(Bad = 1:5, Good = 6:10)
X
# [1] Bad Bad Good Good Bad Good Good Good Good Bad Bad Bad Good Bad Good
# Levels: Bad Good