如何在保留R中的原始列值的同时仅替换选定的值?

时间:2017-12-11 00:26:24

标签: r if-statement

我在数据框(df)中有一个列,如set set。我想专门替换值5和6,并保留set中的原始值。我使用了以下代码

set <- c(3,4,5,5,6,6,10,11)
df$set <- as.factor(df$set)
df$set <- ifelse(df$set == 5 | df$set =="6", 5.5, df$set)

我为df $ set获得的输出是1.0 2.0 5.5 5.5 5.5 5.5 5.0 6.0而不是我预期的输出3,4,5.5,5.5,5.5,5.5,10,11

我尝试按照this示例,但似乎没有效果。

为什么会这样?怎么纠正呢

1 个答案:

答案 0 :(得分:1)

了解levels

创建的as.factor非常重要
df <- data.frame(
  set = c(3, 4, 5, 5, 6, 6, 10, 11)
)

# numeric
ifelse(df$set == 5 | df$set == 6, 5.5, df$set)
# [1]  3.0  4.0  5.5  5.5  5.5  5.5 10.0 11.0

# character
ifelse(df$set == "5" | df$set == "6", "5.5", as.character(df$set))
# [1] "3"   "4"   "5.5" "5.5" "5.5" "5.5" "10"  "11"

# factor
ifelse(df$set == "5" | df$set == "6", "5.5", as.factor(df$set))
# [1] "1"   "2"   "5.5" "5.5" "5.5" "5.5" "5"   "6"

levels(as.factor(df$set))
# [1] "3"  "4"  "5"  "6"  "10" "11"
#
# 1 = "3"
# 2 = "4"
# 3 = "5"
# 4 = "6"
# 5 = "10"
# 6 = "11"