[R] Time difference between two dates/timing

arun smartpink111 at yahoo.com
Thu Jun 14 20:22:27 CEST 2012


Hi,

If you need the difference between two dates from the dataset,

Try this:

 >dat1<-data.frame(DATETIME=c("1/1/2010 0:10","1/1/2010 0:20","1/1/2010 0:30"),HEADER1=c(197.19,203.88,206.56),HEADER2=c(100.08,100.10,100.04))
 >dat1$DATETIME<-strptime(dat1$DATETIME, "%d/%m/%Y %H:%M")

 >difftime(dat1[2,1],dat1[1,1],units="mins")
Time difference of 10 mins
 >difftime(dat1[3,1],dat1[1,1],units="mins")
Time difference of 20 mins

To get the difference between consecutive rows
>diff(dat1$DATETIME)
Time differences in mins
[1] 10 10
attr(,"tzone")
[1] ""




A.K.




----- Original Message -----
From: Rantony <antony.akkara at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, June 14, 2012 6:08 AM
Subject: [R] Time difference between two dates/timing

Hi,


Here, i have a matrix like this

MyMatrix <-

*DATETIME     HEADER1            HEADER2*
1/1/2010 0:10    197.1947                 100.0859
1/1/2010 0:20    203.8811                 100.1013
1/1/2010 0:30    206.564                  100.0433
1/1/2010 0:40    207.9563                   99.9393

i want to get the time difference in minutes between two date.

TimeDiff <- MyMatrix[1,1] -  MyMatrix[2,1] 
TimeDiff <- 10 (need to get like this )

Could you please help me ?


--
View this message in context: http://r.789695.n4.nabble.com/Time-difference-between-two-dates-timing-tp4633353.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list