Reputation: 3774
I use ifelse
with mutate
to conditionally change variable names within a column. For example:
mutate(df, thiscol = ifelse((new == "oldname"), "newname", NA)
But I would like to change it so that instead of variables that don't fit the conditions turning into NA, they just stay as they were.
Upvotes: 2
Views: 1718
Reputation: 887871
If we need to change only some elements and keep the rest as the same, then another option is replace
df %>%
mutate(thiscol = replace(thiscol, new == "oldname", "newname"))
Upvotes: 2