R ifelse to replace values in a column

This should work, using the working example:

var <- c("Private", "Private", "?", "Private")
df <- data.frame(var)
df$var[which(df$var == "?")] = "Private"

Then this will replace the values of “?” with “Private”

The reason your replacement isn’t working (I think) is as if the value in df$var isn’t "?" then it replaces the element of the vector with the whole df$var column, not just reinserting the element you want.

Leave a Comment