Converting dates into minutes in R + Error in as.POSIXlt.character message

249 Views Asked by At

I am pretty new to R. I have a variable with a date and hour automatically created by the survey platform (Qualtrics) named enddate. I would like to convert that variable into minutes from midnight (the dates are shown in this format: 08/03/2020 08:17).

I tried this code I found around the web:

enddate _mins<- hour(dat$enddate) * 60 + minute(dat$enddate) / 60

but I get this error message:

Error in as.POSIXlt.character(x, tz = tz(x)) : 
  character string is not in a standard unambiguous format

I searched about this error and I found the following code to solve the previous issue:

enddate_num <- parse_date_time(dat$enddate, orders = "mdy HM")

I am now getting this error:

Warning message: 1306 failed to parse

I would very much appreciate your help as I don't know what else I can do.

1

There are 1 best solutions below

2
Dave2e On

There is a difftime() function.

Covert the string to a datetime and then use the function:

timestring <-"08/03/2020 08:17" 

difftime(as.POSIXct(timestring, "%d/%m/%Y %H:%M", tz="GMT"), 
         as.POSIXct(timestring, "%d/%m/%Y", tz="GMT"), 
         units="mins")