Calculation of the time difference in R using Lubridate

After converting the coefficients in POSIXCT format, and then applying the date and time format, I want to change the difference between the dates and the two positions pos1 and pos2.

However, when I do this for a specific item, I get the correct answer in the console, but when I perform the operation as a whole, the console displays only the number, and also the date frame reflects these numbers, as you can see.

How can I get the clock in a DataFrame when I try to make a difference? I am using lubridate package, is there any function for this?

Here is an example of code / image data in RStudio describing it

CR_Date <- data.frame(
  pos1="2014-07-01 00:00:00",
  pos2=c("2014-07-01 00:00:00","2014-07-01 10:15:00")
)
CR_Date[] <- lapply(CR_Date,as.POSIXct)
CR_Date

#        pos1                pos2
#1 2014-07-01 2014-07-01 00:00:00
#2 2014-07-01 2014-07-01 10:15:00

CR_Date$pos2[2] - CR_Date$pos1[2]
#Time difference of 10.25 hours
CR_Date$hours <- CR_Date$pos2 - CR_Date$pos1

enter image description here

+4
source share
1 answer

-, lubridate.

-, RStudio , . CR_Date$hours , ,

#Time differences in secs
#[1]     0 36900

head(CR_Date) :

#        pos1                pos2      hours
#1 2014-07-01 2014-07-01 00:00:00     0 secs
#2 2014-07-01 2014-07-01 10:15:00 36900 secs

, .

@Victorp, difftime - :

CR_Date$hours <- with(CR_Date, difftime(pos2,pos1,units="hours") )
CR_Date

#        pos1                pos2       hours
#1 2014-07-01 2014-07-01 00:00:00  0.00 hours
#2 2014-07-01 2014-07-01 10:15:00 10.25 hours
+14

All Articles