将因子中的级别更改为R中具有不同顺序的数字

时间:2016-12-07 21:11:45

标签: r

我的数据集中有一列与下面的因素相似:

response = c("Met Expectations", "Exceeded Expectations", "Exceeded    Expectations", "Unacceptable", NA, "Did not meet Expectations" )
factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

这是我获得的水平。我按照我在数据集中的方式订购了这个。

[1] Met Expectations          Exceeded Expectations     Exceeded Expectations     Unacceptable             
[5] <NA>                      Did not meet Expectations
Levels: Exceeded Expectations Met Expectations Did not meet Expectations Unacceptable

我想将这些级别转换为数字。所以我尝试了这个:

as.numeric(factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable")))

我得到了正确的输出:

[1]  2  1  1  4 NA  3

但是我希望订单是4 3 2 1而不是1 2 3 4.我期待:

Exceeding Expectation to be 4
Met Expectations to be 3
Did not meet Expectations to be 2
Unacceptable to be 1
[1] 3  4  4  1  NA  2

如何更改订单?有没有直接的方法来做其他我可以修改数字后将它们转换为数字。在这个例子中,我可以改变levels参数的顺序并得到我想要的,但是因为我的数据集以这个特定的顺序给出了我的级别:

级别:超出期望满足期望未达到预期不可接受

我试图找出一个简单的方法。

1 个答案:

答案 0 :(得分:1)

创建因子时,请更改为levels = rev(c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))rev将颠倒级别的顺序,从而颠倒分配给类别标签的数值的顺序。

response = c("Met Expectations", "Exceeded Expectations", "Exceeded Expectations", "Unacceptable", NA, "Did not meet Expectations")

response = factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

as.numeric(response)

#[1]  2  1  1  4 NA  3

response = factor(response, levels = rev(c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable")))

as.numeric(response)

#[1]  3  4  4  1 NA  2

如果您不想更改系数变量中级别的顺序,可以执行以下操作:

response = factor(response, levels = c("Exceeded Expectations", "Met Expectations", "Did not meet Expectations", "Unacceptable"))

# Convert to numeric with numeric order of factor reversed
5 - as.numeric(response)

#[1]  3  4  4  1 NA  2

如果级别数可能不同,您还可以动态计算级别数:

# Convert to numeric with numeric order of factor reversed
length(unique(na.omit(response))) + 1 - as.numeric(response)