Search code examples
rdplyrmutate

R Make changes across multiple columns based off value in another column


I have a data frame where unknown birthdates are '1753-01-01'. The birthdates are used to calculate a current age and a due status. I'd like to change specific columns to NA if the birthdate is unknown and leave all others as is if the Birthdate is not unknown.

ID <- c("001", "002")
birthdate <- c("1753-01-01", "2019-01-10")
currentage <- c("98659", "1682")
duestatus <- c("due", "due")
gender <- c("F", "F")

df1 <- data.frame(ID, birthdate, currentage, duestatus, gender)
df1
   ID  birthdate currentage duestatus gender
1 001 1753-01-01      98659       due      F
2 002 2019-01-10       1682       due      F

The desired output would be like this

   ID  birthdate currentage duestatus gender
1 001         NA         NA        NA      F
2 002 2019-01-10       1682       due      F

I have started playing with dplyr::mutate but just can't quite get it right. The birthdate is the only value that won't change, where as current_age and duestatus will change depending on the date the code is run so ideally the code would be based off something like: when the birthdate is '1753-01-01' then change birthdate, currentage and duestatus to NA

df1 <- df1%>% mutate(case_when(birthdate == '1753-01-01' ~ NA))

Solution

  • df1 %>%
      mutate(across(birthdate:duestatus, 
                    ~if_else(birthdate == '1753-01-01', NA, .)))
    

    Result

       ID  birthdate currentage duestatus gender
    1 001       <NA>       <NA>      <NA>      F
    2 002 2019-01-10       1682       due      F