Changing a NA value

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

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(is.na(col1) == TRUE, "Y", col1))
1 Like

Another way is with the function replace_na.

library(tidyverse)

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

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

df %>%
  replace_na(list(col1="Y"))

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.