Home > Blockchain >  How to import date from CSV in dd/mm/yyyy hh:mm format in to general number string in R
How to import date from CSV in dd/mm/yyyy hh:mm format in to general number string in R

Time:12-08

Hope you can help i have massive CSV file i need to import in to R manipulate and export to excel, all other data is importing and manipulating fine apart from the Date format, the CSV is supplied (and cant be changed) with all dates with dd/mm/yyyy hh:mm, i need a way to strip it down to dd/mm/yyyy,(dd/mm/yy) all methods i have tried so far have altered the date to mm/dd/yyyy of give me multiple errors.

The only work around i have found is to convert the data in the CSV in to General format before importing it however the "live" CSV are to big to open and convert.

Any help would be great

CodePudding user response:

One potential solution could be to use lubridate to parse the text strings of the date/time columns after import. From this you can extract the date and time (using date() and hms::as_hms()):

library(readr)
library(dplyr)
library(lubridate)


read_csv("Date_time\n
         01/09/2021 19:30\n
         19/12/2020 12:45\n
         16/03/2019 00:15") %>% 
  mutate(Date_time = dmy_hm(Date_time),
         Date = date(Date_time),
         Time = hms::as_hms(Date_time))

#> # A tibble: 3 x 3
#>   Date_time           Date       Time  
#>   <dttm>              <date>     <time>
#> 1 2021-09-01 19:30:00 2021-09-01 19:30 
#> 2 2020-12-19 12:45:00 2020-12-19 12:45 
#> 3 2019-03-16 00:15:00 2019-03-16 00:15

This at least gives you tidy and workable data imported into R, able to be formatted for printing. Does this reach your solution? If it's not working on your data then perhaps post a small sample (or representative sample) as an example to try and get working.

Created on 2021-12-07 by the reprex package (v2.0.1)

  • Related