Search code examples
rmaxlubridate

Problem with "max" function and dates with R


I have a dataframe with dates and I simply want to know the max date:

dates <- data.frame( randomdate = c("30-11-2018", "25-10-2018", "10-12-2015", "10-01-2020", "15-2-2013", "17-8-1999") )

So I convert them to date format:

dates$randomdate <- as.POSIXct(dates$randomdate,format = "%d-%m-%Y")

And then I ask to create a column with the last date:

dates$last_date= as.Date(max(dates$randomdate))

The problem is that the last date in my dataset in the 10th of January 2020 and the max function just invent a date and gives me the "9th of January 2020" (this date isn't even in my dataset)

If someone can help me, maybe I don't understand how the max function works...

Kind regards


Solution

  • As already mentioned in comments and in other answer you should use as.Date to convert to dates since you have dates. However, the reason why you get max date as "9th of January 2020" is because as.POSIXct converts object to your local timezone whereas as.Date uses "UTC" timezone.

    You can use the same timezone in both the function to resolve it.

    dates$randomdate <- as.POSIXct(dates$randomdate,format = '%d-%m-%Y', tz = 'UTC')
    dates$last_date <- max(as.Date(dates$randomdate))