My first post was intended to show how to test for missing values. Here's part of your original example:
df_temp<-df_temp %>%
mutate(age_years_correct = ifelse(age_years_correct==NA,
age_years,
age_years_correct))
In order to properly test for NA, age_years_correct==NA should be changed to is.na(age_years_correct). So the code should be:
df_temp<-df_temp %>%
mutate(age_years_correct = ifelse(is.na(age_years_correct),
age_years,
age_years_correct))
Here's an example of what's going wrong when you test for NA using == vs. is.na():
x = c(1, 3, NA, 5)
x==NA
#> [1] NA NA NA NA
is.na(x)
#> [1] FALSE FALSE TRUE FALSE
Created on 2021-03-08 by the reprex package (v1.0.0)
Also, given your examples, I'm not certain, but you might be able to use the coalesce function to save some typing:
df_temp <- dt_temp %>%
mutate(age_years_correct = coalesce(floor(age_months/12), age_years))
The code above will set age_years_correct to floor(age_months/12) when that value is not missing, or age_years otherwise. A coalesce example:
library(tidyverse)
x = c( 1, 3, NA, 5, NA, 7)
y = c(NA, 30, NA, 50, 60, NA)
coalesce(x,y)
#> [1] 1 3 NA 5 60 7
coalesce(y,x)
#> [1] 1 30 NA 50 60 7
Created on 2021-03-08 by the reprex package (v1.0.0)