-2

This is the input

library(lubridate)
        library(tidyverse)
        library(stringr)
        library(dplyr) 

        dataset <- data.frame(A = c("12345 Software Inc","12346 Software In","123463 Software In"),
                      B = c(12345,1234523,234234234),
                      C = c(ABC-1244, DEF-1134,ACF-1234),
                      X.Y DATE = c("1-May-2019","2-Jun-2020","3-July-2020"),
                      XX.YY DATE = c("1-May-2019","5-Jun-2020","4-July-2020"),
                      Check_DATE = c("1-May-2019","2-Jun-2020","3-July-2020")
)

        date_parser <- function(column){
              if(any(grepl("[-]",column))){
                format(parse_date_time(column,orders = c("bdy","dmy","dby","ymd","mdy","ydm","%d-%m-%Y %H:%M:%S","%m-%d-%Y %H:%M:%S","%Y-%m-%d %H:%M:%S","%d-%m-%Y","%d-%b-%Y")),"%d-%m-%Y")
              }
              else column
            }


            date_format_change <- function(dataset1) { 
              data <- dataset1 %>% 
                mutate_at(-1,~ stringr::str_replace_all(.,"\\/","-")) %>% 
                purrr::map_dfr(~date_parser(.))
              View(data)
            }
            date_format_change(dataset)

Output Received : [![enter image description here][2]][2]


  [1]: https://i.stack

.

The Date has not changed. and enter image description here there columns have become NA. I want the output should not have any NA columns and date should be DD-MM-YYYY

  • 1
    It would be ideal if you could specify in words what you are trying to achieve and also share a part of your data using dput https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – desval May 01 '20 at 16:37
  • Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans May 01 '20 at 16:48
  • dataset <- data.frame(A = c("12345 Software Inc","12346 Software In","123463 Software In"), B = c(12345,1234523,234234234), X.Y DATE = c("1-May-2019","2-Jun-2020","3-July-2020"), XX.YY DATE = c("1-May-2019","5-Jun-2020","4-July-2020"), Check_DATE = c("1-May-2019","2-Jun-2020","3-July-2020") ) – Questions Ask May 01 '20 at 16:49
  • The above code returns NA for some columns and doesn't change the format of the DATE columns. The date columns should be '01-03-2020' format i.e(DD-MM-YYYY) – Questions Ask May 01 '20 at 17:03
  • is this clear ? – Questions Ask May 01 '20 at 17:17

1 Answers1

0

From the above comments I take it your task is (i) to detect the columns that contain dates and (ii) to change the format of the dates. Here's a solution that uses grepl to match dates and a apply and lubridate to change the date format:

library(lubridate)

dataset[apply(dataset, 2, function(x) all(grepl("\\d{1,2}-[A-z]{3,4}-\\d{4}", x)))] <-
  apply(dataset[apply(dataset, 2, function(x) all(grepl("\\d{1,2}-[A-z]{3,4}-\\d{4}", x)))], 2, 
        function(x) format(parse_date_time(x, orders = "%d-%b-%Y"), "%d-%m-%Y"))

Result:

dataset
                   A         B   X.Y_DATE XX.YY_DATE Check_DATE
1 12345 Software Inc     12345 01-05-2019 01-05-2019 01-05-2019
2  12346 Software In   1234523 02-06-2020 05-06-2020 02-06-2020
3 123463 Software In 234234234 03-07-2020 04-07-2020 03-07-2020

Data:

dataset <- data.frame(A = c("12345 Software Inc","12346 Software In","123463 Software In"), 
                      B = c(12345,1234523,234234234), 
                      X.Y_DATE = c("1-May-2019","2-Jun-2020","3-July-2020"), 
                      XX.YY_DATE = c("1-May-2019","5-Jun-2020","4-July-2020"), 
                      Check_DATE = c("1-May-2019","2-Jun-2020","3-July-2020"))
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34