When i use difftime() in R and what i think is the proper setup I still get strange time values

78 Views Asked by At

I load a dataset from excel

library(readxl)
df<-read_excel("excel_file.XLSX")

In the file there is a separate date column as Posixct

str(df$datecol)

I also have a time column that in R gets loaded as a date time. To bring it back as time I do........

df$Timecol<-format(df$Timecol,"%H:%M:%S")

 

However it turns into a character. This is where i think the problem lies

str(STOP_DATA$`Stop Frisk Time`)

I would think this part resolves the situation

df$merge_date_time<-as.POSIXct(paste(df$Datecol, df$TimeCol), format="%Y-%m-%d %H:%M:%S")

The date and time is then combined. What i want to do now is reference a timestamp column that is a Poxict data type.

str(df$Timestamp)

I would like to then find the time difference between them

df$TIME_SINCE <- difftime(df$Timestamp, df$merge_date_time, tz="UTC", units = "mins" )

but I end up with weird numbers that don't make sense. My guess its the Character data type for time. Does anyone know how to solve this?

1

There are 1 best solutions below

0
On

I ended up finding out that this works

df$date_time<-paste(df$date, format(as.POSIXct(df$time), '%T'))

I removed the portion below from the script as it changed the file into a character.

df$Timecol<-format(df$Timecol,"%H:%M:%S")

I accepted the obscure POSIXT default with the proper time and odd dates (1899-12-31) and what the script did was replace 1899-12-31 with the proper correstponding df$date column.