使用dplyr条件变异仅适用于某些行

时间:2017-03-30 18:51:49

标签: r dplyr

我使用ifelsemutate来有条件地更改列中的变量名称。例如:

mutate(df, thiscol = ifelse((new == "oldname"), "newname", NA)

但是我想改变它,以便不是那些不适合变成NA的条件的变量,它们只是保持不变。

2 个答案:

答案 0 :(得分:3)

mutate(df, thiscol = ifelse(new == "oldname"), "newname", thiscol)

答案 1 :(得分:2)

如果我们只需要更改一些元素并保持其余元素相同,那么另一个选项是replace

df %>%
    mutate(thiscol = replace(thiscol, new == "oldname", "newname"))