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.
You have been asked before (by me!) to give reproducible examples using dput()... You might need the diff() function. Michael On Thu, Jun 14, 2012 at 5:08 AM, Rantony <antony.akkara at ge.com> wrote:> 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.
First thing is to supply data in a usable form See ?dput for one easy way of doing it. In any case, assuming those dates and times are character values something like this should work but not tested on your data. Assuming the data frame is called dtime dtime[,1] <- strptime(dtime[,1], "%d/%m/%Y %H:%M")) diff(dtime[,1]) John Kane Kingston ON Canada> -----Original Message----- > From: antony.akkara at ge.com > Sent: Thu, 14 Jun 2012 03:08:25 -0700 (PDT) > To: r-help at r-project.org > 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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
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.