date time in R help

Hi R community

I always have problems with the date time in r

structure(list(ï..Item = c("851375", "851375", "851375", "851375",
"851375", "851375"), Assortment = c("", "", "", "", "", ""),
Item.group = c("Breast", "Breast", "Breast", "Breast", "Breast",
"Breast"), Customer = c("D17", "D17", "D17", "D17", "D17",
"D17"), Date = c("15-08-2020", "16-01-2018", "24-01-2018",
"27-05-2020", "14-10-2020", "21-08-2020"), Sales = c("374",
"320", "221", "430", "307", "190"), Campaign.flag = c(0L,
0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 6L), class = "data.frame")

str of the date is a chr and i want the date format to be day;month;year and save it as a date.

used the following code:
df$Date <- ymd(df$Date)
df <- arrange(df, Date)

but it returns NA

If you can make a bit more of a reprex (see here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners ) I can have a look. I would recommend lubridate for any of these things (https://raw.githubusercontent.com/rstudio/cheatsheets/master/lubridate.pdf)

Your dates aren't formatted as "ymd". They are "dmy". Consider this code:

dat = structure(list(ï..Item = c("851375", "851375", "851375", "851375",
                           "851375", "851375"), Assortment = c("", "", "", "", "", ""),
                Item.group = c("Breast", "Breast", "Breast", "Breast", "Breast",
                               "Breast"), Customer = c("D17", "D17", "D17", "D17", "D17",
                                                       "D17"), Date = c("15-08-2020", "16-01-2018", "24-01-2018",
                                                                        "27-05-2020", "14-10-2020", "21-08-2020"), Sales = c("374",
                                                                                                                             "320", "221", "430", "307", "190"), Campaign.flag = c(0L,
                                                                                                                                                                                   0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 6L), class = "data.frame")

# check initial data
class(dat$Date)
#> [1] "character"
dat
#>   ï..Item Assortment Item.group Customer       Date Sales Campaign.flag
#> 1  851375                Breast      D17 15-08-2020   374             0
#> 2  851375                Breast      D17 16-01-2018   320             0
#> 3  851375                Breast      D17 24-01-2018   221             0
#> 4  851375                Breast      D17 27-05-2020   430             0
#> 5  851375                Breast      D17 14-10-2020   307             0
#> 6  851375                Breast      D17 21-08-2020   190             0

# change
dat$Date = lubridate::dmy(dat$Date)
dat = dplyr::arrange(dat, Date)
class(dat$Date)
#> [1] "Date"
dat
#>   ï..Item Assortment Item.group Customer       Date Sales Campaign.flag
#> 1  851375                Breast      D17 2018-01-16   320             0
#> 2  851375                Breast      D17 2018-01-24   221             0
#> 3  851375                Breast      D17 2020-05-27   430             0
#> 4  851375                Breast      D17 2020-08-15   374             0
#> 5  851375                Breast      D17 2020-08-21   190             0
#> 6  851375                Breast      D17 2020-10-14   307             0

Created on 2022-01-12 by the reprex package (v2.0.1)

dat$Date = lubridate::dmy(dat$Date)
dat = dplyr::arrange(dat, Date)

gives me an error:

Warning message:
All formats failed to parse. No formats found.

and the entire date vector is NA

the original date was dmy then u write the following code:

change

dat$Date = lubridate::**dmy(**dat$Date)
dat = dplyr::arrange(dat, Date)

and the date is changed to ymd ? u wrote "lubricate::dmy(df$Date) that is the opposite?

When R has an object that is a date, it formats it "ISO" style, or YYYY-MM-DD hh:mm:ss. Look at the below example; column "x" is a character string in dmy, but when converted to a "dttm" object it is reformatted to be ymd.

tibble::tibble(x = "12/01/2022 12:00:00") |> 
+   dplyr::mutate(y = lubridate::dmy_hms(x))
# A tibble: 1 x 2
  x                   y                  
  <chr>               <dttm>             
1 12/01/2022 12:00:00 2022-01-12 12:00:00

My code works with the example data you provided on my machine. All you need to do to convert a character string that represents a date into an actual datetime object is to find the {lubridate} function that matches the format of your character string. Perhaps the {lubridate} documentation will help:

i think I understand what your saying, but when i type the code:
dmy(df$Date)

i get the output:

which is ymd

why is that?

and when i type ymd i get:

In your first screenshot you have used dmy() to convert your character strings into datetime objects.

The way R prints this is YYYY-MM-DD regardless of how it was formatted originally.

You can't use ymd() on your data because it isn't formatted as YYYY-MM-DD as a character string.

To prove the conversion works, try:

class(df$Date)
df$Date <- dmy(df$Date)
class(df$Date)

The first class() call will show it is a "character", the second will show "Date".

Or look at the following example:

lubridate::dmy("31-05-2020") # correct
#> [1] "2020-05-31"

lubridate::ymd("31-05-2020") # wrong
#> Warning: All formats failed to parse. No formats found.
#> [1] NA

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.