使用setNames替换R dataframe列中的多个值时出现奇怪的行为

时间:2016-12-14 14:19:53

标签: r dataframe replace

我希望使用R替换setNames数据框中的多个值,如https://stackoverflow.com/a/7548031/4169924中所述,但它会给出(看似)意想不到的结果:

df1 <- data.frame(Measure = c("Min", "min", "Minimum"), Value = c(1,2,3))
map1 = setNames(c("Minimum", "Minimum", "Minimum"), c("Min", "min", "Minimum"))
df1$Measure <- map1[df1$Measure]
df1

给出预期结果:

  Measure Value
1 Minimum 1
2 Minimum 2
3 Minimum 3

然而,

df2 <- data.frame(Measure = c("Min", "min", "Minimum", "MaxVal"), Value = c(1,2,3,4))
map2 = setNames(c("Minimum", "Minimum", "Minimum", "MaxVal"), c("Min", "min", "Minimum", "MaxVal"))
df2$Measure <- map2[df2$Measure]
df2

我明白了:

  Measure Value
1 Minimum 1
2 Minimum 2
3 MaxVal  3
4 Minimum 4

第3行和第4行的测量似乎被错误地替换。为什么呢?

1 个答案:

答案 0 :(得分:3)

因为第一列是一个因子,而不是一个字符向量。使用以下命令创建它:

df2 <- data.frame(Measure = c("Min", "min", "Minimum", "MaxVal"), Value = c(1,2,3,4), stringsAsFactors=FALSE)

或者在此步骤转换:

df2$Measure <- map2[as.character(df2$Measure)]