我有一个数字向量,让我们说:
x <- rep(1:6, 300)
Run Code Online (Sandbox Code Playgroud)
我想要做的是重新编码矢量,使得6 = 1,5 = 2,4 = 3,3 = 4,2 = 5,1 = 6.我不想创造一个因素.
到目前为止,我所尝试的所有内容都因为顺序而给出了错误的计数,即:
x[x == 6] <- 1
x[x == 5] <- 2 ## Lines that follow where x[x == 2] removes 5's entirely from counts.
Run Code Online (Sandbox Code Playgroud)
注意:我知道汽车包装,但更喜欢使用基本R来解决这个问题.