这个答案可能很明显(我希望如此),但我一直只找到复杂的解决方案。我想做的是根据另一个因素的水平有条件地重新评估一个因素。
这是使用 mtcars 数据集的示例:
data(mtcars)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$am <- as.factor(mtcars$am)
table(mtcars$gear, mtcars$am) # examining the levels
levels(mtcars$gear)
# [1] "3" "4" "5"
levels(mtcars$am)
"0" "1"
现在在那些齿轮级别为“5”的汽车中,我如何将新的“齿轮”级别“6”分配给那些“am”级别为“1”的汽车,同时保留因子级别“3”, 4","5" 代表“齿轮”?这是一个更简单的示例,但考虑到我的数据集的复杂性,我更愿意将向量保留为因子(例如,而不是转换为数字并返回)。