0

Hey I have a column of birth dates in this format:

dob
19881011
19590223
19860407
19710921
19640213

I need to edit the column to this format:

dob
1988-10-11
1959-02-23
1986-04-07
1971-09-21
1964-02-13

I came across similar problems being solved with gsub and regex but was able to apply it to my problem. Can someone recommend a solution?

2 Answers2

1

The ymd from lubridate can correctly parse it to Date class if it is numeric or character

library(dplyr)
library(lubridate)
df1 <- df1 %>%
    mutate(dob = ymd(dob))

-output

df1
#     dob
#1 1988-10-11
#2 1959-02-23
#3 1986-04-07
#4 1971-09-21
#5 1964-02-13

It is not a regex problem as converting to Date class is straightforward with as.Date and format argument. If we need a regex option

sub("(....)(..)(..)", "\\1-\\2-\\3", df1$dob)

data

df1 <- structure(list(dob = c(19881011L, 19590223L, 19860407L, 19710921L, 
19640213L)), class = "data.frame", row.names = c(NA, -5L))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

A base R option using as.Date

transform(
  df,
  dob = as.Date(as.character(dob), "%Y%m%d")
)

gives

         dob
1 1988-10-11
2 1959-02-23
3 1986-04-07
4 1971-09-21
5 1964-02-13

Data

> dput(df)
structure(list(dob = c(19881011L, 19590223L, 19860407L, 19710921L, 
19640213L)), class = "data.frame", row.names = c(NA, -5L))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81