set dates manually or automatically

set_date(.data, ..., date_fn = lubridate::ymd)

Arguments

.data

dataframe

...

tidyselect

date_fn

a function to convert to a date object

Value

tibble

Details

note: can be called without any ... arguments and instead automatically determines which character columns are actually dates, then proceeds to set them. It checks for the date specified in date_fn and also ymd_hms. On auto detect mode, it sets ymd_hms output to ymd dates instead of datetimes with hms. This is because of the common occurrence of trying to extract a ymd date from an excel workbook, and having it come with extra 00:00:00. If you need a datetime, manually supply the appropriate lubridate function.

Auto mode is experimental. Commonly detected error is a long character string of integers being interpreted as a date.

Examples


tibble::tibble(date_col1 = c("20190101", "20170205"),
date_col2 = c("20201015", "20180909"),
not_date_col = c("a345", "b040")) -> t1

t1
#> # A tibble: 2 × 3
#>   date_col1 date_col2 not_date_col
#>   <chr>     <chr>     <chr>       
#> 1 20190101  20201015  a345        
#> 2 20170205  20180909  b040        

t1 %>%
set_date()
#> # A tibble: 2 × 3
#>   date_col1  date_col2  not_date_col
#>   <date>     <date>     <chr>       
#> 1 2019-01-01 2020-10-15 a345        
#> 2 2017-02-05 2018-09-09 b040        

t1 %>%
set_date(date_col1)
#> # A tibble: 2 × 3
#>   date_col1  date_col2 not_date_col
#>   <date>     <chr>     <chr>       
#> 1 2019-01-01 20201015  a345        
#> 2 2017-02-05 20180909  b040