Changing a NA value

I am searching for the formula to change a 'NA' value with a value such as 'Y' within a column of a dataset.

Assuming your column is called col1 and your dataset is df, you can do this:

# Replace NAs with "Y", otherwise keep it as the existing value
df <- df %>% 
  mutate(col1 = ifelse( == TRUE, "Y", col1))
1 Like

Another way is with the function replace_na.


# this replaces all values of NA with a "Y"
df %>% 

# this replaces all values of NA with a "Y" for variable col1

df %>%

1 Like

Thanks very much. The code worked just fine..

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.