我希望使用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行的测量似乎被错误地替换。为什么呢?
答案 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)]