我使用ifelse
和mutate
来有条件地更改列中的变量名称。例如:
mutate(df, thiscol = ifelse((new == "oldname"), "newname", NA)
但是我想改变它,以便不是那些不适合变成NA的条件的变量,它们只是保持不变。
答案 0 :(得分:3)
mutate(df, thiscol = ifelse(new == "oldname"), "newname", thiscol)
答案 1 :(得分:2)
如果我们只需要更改一些元素并保持其余元素相同,那么另一个选项是replace
df %>%
mutate(thiscol = replace(thiscol, new == "oldname", "newname"))