将因子转换为整数,同时保持因子级别排序 [英] Convert factor to integer while maintaining factor level ordering

查看:57
本文介绍了将因子转换为整数,同时保持因子级别排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R数据帧,其中列之一是其级别具有隐式排序的因子。 如何通过以下方式将因子级别转换为特定的整数

I have an R dataframe where one of the columns is a factor whose levels have an implicit ordering. How can I convert the factor levels to specific integers in the following manner:


  • 强烈不同意- > 1

  • 有些不同意-> 2

  • 中性-> 3

  • 完全同意-> 4

  • 完全同意-> 5

  • "Strongly disagree" --> 1
  • "Somewhat disagree" --> 2
  • "Neutral" --> 3
  • "Somewhat agree" --> 4
  • "Strongly agree" --> 5

例如,这是我的数据框:

For example, here is my data frame:

agree <- c("Strongly agree", "Somewhat disagree", "Somewhat agree",
           "Neutral", "Strongly agree", "Strongly disagree", "Neutral")
age <- c(41, 35, 29, 42, 31, 22, 58)

df <- data.frame(age, agree)
df
#   age             agree
# 1  41    Strongly agree
# 2  35 Somewhat disagree
# 3  29    Somewhat agree
# 4  42           Neutral
# 5  31    Strongly agree
# 6  22 Strongly disagree
# 7  58           Neutral

str(df)
# 'data.frame': 7 obs. of  2 variables:
#  $ age  : num  41 35 29 42 31 22 58
#  $ agree: Factor w/ 5 levels "Neutral","Somewhat agree",..: 4 3 2 1 4 5 1

现在,我想转换同意列将成为整数列。

Now, I would like to convert the agree column to be an integer column using the mapping that I showed above.

我已经搜索了有关将因子转换为整数的其他问题,但它们与

I already searched these other questions about converting factor to integer, but they do not related to maintaining the factor ordering.

"How to convert a factor to an integer\numeric without a loss of information?"

将因子转换为整数

在数据框中将因子转换为整数

解决方案

您需要先定义因子的顺序:

You need to define the order of factors first:

ordering <- c("Strongly disagree", "Somewhat disagree", "Neutral", "Somewhat agree", "Strongly agree")

然后,当您首次创建因子时,应使用该定义:

Then, when you first create your factor, you should use that definition:

agreeFactor <- factor(agree, levels = ordering)

然后,您应该能够获得订购的因子:

Then, you should be able to get your ordered factor:

as.numeric(agreeFactor)

在使用as.numeric()时,您也可以仅应用顺序,但是如果您以后决定检索数字矢量而忘记应用 levels =参数,则可能导致不一致。

You can also just apply the order when using as.numeric(), but this can lead to inconsistencies if you decide to later retrieve your numeric vector and forget to apply the "levels = " argument.

e:如果要将数字直接导入到数据框中,只需使用:

e: If you want to directly import the numeric into your dataframe, simply use:

df$agree <- as.numeric(factor(df$agree, levels = ordering))

这篇关于将因子转换为整数,同时保持因子级别排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆