You can use this approach to get what you need:
df = within(df, {
variable[variable == "string"] = "string2"
variable[is.na(variable)] = mean(variable)
})
The trick here is that you can create a subset using []
and assign values again to that subset using [] =
.
2
solved How to replace a value by another within a variable in R?