我想使用“汽车”库将数值重新编码为序数变量。
但是,似乎我无法在我的重新编码功能中使用“否则”。
在这种情况下,如果我想将不在(0.0 到 9.0)范围内的任何其他值编码为 999,我可以知道如何修复代码吗?
谢谢你。
这是我的代码:
newvar <- apply(abc, 2, function(x) {x <-
recode(x,
"0.0:1.5=1;
1.6:1.7=2;
1.8:1.9=3;
2.0:2.4=4;
2.5:2.9=5;
3.0:3.4=6;
3.5:3.9=7;
4.0:4.4=8;
4.5:9.0=9;
otherwise =999"); x})
message: Error in eval(expr, envir, enclos) : object 'otherwise' not found
想要有如下的新变量(def):
abc def
0.5 1
3.5 7
11.5 999
5.8 9