0

Just started learning R and I would like to convert a string of a total of 10 characters (yymmdd and some random numbers) to date format.

Example:

       Numbers
1.    6010111234
2.    7012245675
3.    9201015678
4.    0404125689

Desired outcome:

        Numbers            Dates
1.    6010111234        1960-10-11
2.    7012245675        1970-12-24
3.    9201015678        1992-01-01
4.    0404125689        2004-04-12

I will able to do this easily in excel with the formula Dates, left and right:

DATES(LEFT(Numbers,2),RIGHT(LEFT(Numbers,4),2), RIGHT(LEFT(Numbers,6),2))

I have also tried using as.Date(substr(df$Numbers, 1,6), format=%y%m%d).

However, the results are not what I wanted. The results will be some 4-5 digit numbers.

Can anyone help? Thanks!

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Kenny
  • 1

1 Answers1

1

If you don't like what dates are put into 20th, respective 21st century by as.Date(..., format = '%y%m%d'), you can write your own variant:

nums <- c('6010111234', '7012245675', '9201015678', '0404125689')
breakpoint <- '30'

dplyr::if_else(substr(nums, 1, 2) >= breakpoint , 
       as.Date(paste0('19', substr(nums, 1, 6)), '%Y%m%d'),
       as.Date(paste0('20', substr(nums, 1, 6)), '%Y%m%d')
)

#"1960-10-11" "1970-12-24" "1992-01-01" "2004-04-12"

dplyr::if_else is used since ifelse() coerces dates to numeric, see e.g. this question

Ape
  • 1,159
  • 6
  • 11