Trying to convert dates stored as numbers in a column

I have this column that is a mixture of "NO" and numbers that are really just badly imported dates.
Is there any way to keep the "NO" but modify the numbers to be the correct date?

# A tibble: 114.167 × 1
 1 NO           
 2 NO           
 3 NO           
 4 41064        
 5 NO           
 6 NO           
 7 NO           
 8 NO           
 9 NO           
10 41019     

I tried this code but it didnt work:

base %>%
  select(hpv_pre_fecha) %>%
  mutate(hpv_pre_fecha2= if_else(str_detect(hpv_pre_fecha,
                                hpv_pre_fecha %>%
                                  as.numeric() %>%
                                  excel_numeric_to_date() %>%,

I know you said you wanted to keep the "NO"s, but in case you are also fine with NAs instead of NOs:

janitor::convert_to_date(data$hpv_pre_fecha, string_conversion_failure = "warning")

This would give you a column with dates converted and NA for anything it could not convert.

Hi, thanks for the answer, but I need to keep the "NO"

OK, in that case just keep on processing:

janitor::convert_to_date(data$hpv_pre_fecha, string_conversion_failure = "warning") |> as.character() |> replace_na("NO")

But the downside is that your dates are now characters...

This topic was automatically closed 42 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.