本文介绍了R使用ifelse来改变因子值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个因子变量列。我需要更改因子是某个很少出现的级别的所有单元格的值。我正在使用以下代码,但它似乎不起作用:
I have a column that's a factor variable. I need to change the value of all cells where the factor is a certain rarely occurring level. I'm using the following code but it doesn't seem to be working:
test2$timeFactor <- ifelse(test2$timeFactor == '94', '-1000', test2$timeFactor)
我也是试过:
test2$timeFactor <- factor(ifelse(test2$timeFactor == '94', '-1000', test2$timeFactor))
但似乎都不起作用。我在这里找不到任何明显的东西?
but neither seems to work. Anything obvious I'm missing here?
推荐答案
你最好更改级别
:
set.seed(1)
x <- factor(sample(letters[1:3],10,replace=T))
x
[1] a b b c a c c b b a
Levels: a b c
levels(x)[which(levels(x)=="c")] <- "z"
x
[1] a b b z a z z b b a
Levels: a b z
这篇关于R使用ifelse来改变因子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!