0

Creating a function that identifies only date columns in a data frame. And change their format to DD-MMM_YYYY(01-Jan-2020)

 data <- data.frame(Name = c("A","B","C"),
                        Col2 = c("01-01-2020","01-Feb-2020","31/05/2020"),
                col3 = c("05-Feb-2020","13/April/2020","2020-09-17"), 
                        Col4 = c("XYZ","REW","RRRR"),
                        col5 = c("01-Mar-2020","01/01/2019","2020-01-12")) 

func <- function(data) { }

Expected output

enter image description here

NOTE : The function needs to identify which columns are date and change its format to yyyy-mm-dd

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

2

Using the tidyverse(see Note):

    date_parser <- function(column){
   if(any(grepl("[-]",column))){

 format(parse_date_time(column,orders = c("dmy","dby","ymd")),"%d-%b-%Y")
     }

   else column
 }
   df %>% 
   mutate_at(-1,~ stringr::str_replace_all(.,"\\/","-")) %>% 
  purrr::map_dfr(~date_parser(.))
# A tibble: 3 x 5
  Name  Col2        col3        Col4  col5       
  <chr> <chr>       <chr>       <chr> <chr>      
1 A     01-Jan-2020 05-Feb-2020 XYZ   01-Mar-2020
2 B     01-Feb-2020 13-Apr-2020 REW   01-Jan-2019
3 C     31-May-2020 17-Sep-2020 RRRR  12-Jan-2020

NOTE:

This is not an actual date, you can convert it again with lubridate although I doubt dttm supports mixed dates. Hence as far as I know, you'll still resort to characters.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57