How to replace a value by another within a variable in R?

You can use this approach to get what you need:

df = within(df, {
   variable[variable == "string"] = "string2"
   variable[] = mean(variable)

The trick here is that you can create a subset using [] and assign values again to that subset using [] =.

Browse More Popular Posts

Leave a Comment