我有一个 R 数据框,其中一列是一个因子,其级别具有隐式排序。 如何通过以下方式将因子水平转换为特定整数 :
例如,这是我的数据框:
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
现在,我想使用上面显示的映射将
agree
列转换为整数列。我已经搜索了有关将因子转换为整数的其他问题,但它们与维护因子排序无关。
“How to convert a factor to an integer\numeric without a loss of information? ”
“Convert factor to integer ”
“Convert factor to integer in a data frame ”
最佳答案
您需要先定义因子的顺序:
ordering <- c("Strongly disagree", "Somewhat disagree", "Neutral", "Somewhat agree", "Strongly agree")
然后,当您第一次创建因子时,您应该使用该定义:
agreeFactor <- factor(agree, levels = ordering)
然后,您应该能够获得您的有序因子:
as.numeric(agreeFactor)
您也可以在使用 as.numeric() 时只应用顺序,但如果您决定稍后检索数字向量并忘记应用“levels =”参数,这可能会导致不一致。
e:如果您想直接将数字导入您的数据框中,只需使用:
df$agree <- as.numeric(factor(df$agree, levels = ordering))
关于r - 将因子转换为整数,同时保持因子级别排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38621334/