Hi all, I have the following problem: I have a csv-file consisting of timestamp values (no dates), e.g.: Timestamp1;Timestamp2; 05:24:43;05:25:05; 15:47:02;15:47:22; 18:36:05;18:36:24; 15:21:24;15:22:04; I need a vector with the difference of the two timestamps, so I read the data with the read.csv-function: myObj <- read.csv("file.csv",header=TRUE,sep=";"). I have then tried unsuccessfully to convert the data to time format (e.g. using the strptime function like date1 <- strptime(myObj[0],format="%H:%M:%S")) to be able to perform a datediff operation. I would be very grateful if anybody could give me some assistance. Thanks in advance, Ieyasu -- View this message in context: http://www.nabble.com/diff-of-two-timestamps-tp25267603p25267603.html Sent from the R help mailing list archive at Nabble.com.
Try this:> Lines <- "Timestamp1;Timestamp2;+ 05:24:43;05:25:05; + 15:47:02;15:47:22; + 18:36:05;18:36:24; + 15:21:24;15:22:04;"> > library(chron) > DF <- read.csv(textConnection(Lines), sep = ";", as.is = TRUE) > times(DF$Timestamp2) - times(DF$Timestamp1)[1] 00:00:22 00:00:20 00:00:19 00:00:40 See the relevant article in R News 4/1 and its references for more info. On Wed, Sep 2, 2009 at 7:50 PM, sugo<ieyasu at sugimoto.at> wrote:> > Hi all, > > I have the following problem: I have a csv-file consisting of timestamp > values (no dates), e.g.: > Timestamp1;Timestamp2; > 05:24:43;05:25:05; > 15:47:02;15:47:22; > 18:36:05;18:36:24; > 15:21:24;15:22:04; > > I need a vector with the difference of the two timestamps, so I read the > data with the read.csv-function: > myObj <- read.csv("file.csv",header=TRUE,sep=";"). > > I have then tried unsuccessfully to convert the data to time format (e.g. > using the strptime function like date1 <- > strptime(myObj[0],format="%H:%M:%S")) to be able to perform a datediff > operation. > > I would be very grateful if anybody could give me some assistance. > > Thanks in advance, > Ieyasu > -- > View this message in context: http://www.nabble.com/diff-of-two-timestamps-tp25267603p25267603.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. >
Try require(chron) dat <- read.table(textConnection("Timestamp1 Timestamp2 05:24:43 05:25:05 15:47:02 15:47:22 18:36:05 18:36:24 15:21:24 15:22:04"), T) closeAllConnections() str(dat) dat[,1] <- times(dat[,1]) dat[,2] <- times(dat[,2]) # numeric as.numeric(dat[,2] <- times(dat[,2]) ) # test to get seconds from days 86400*as.numeric(dat[,2]-dat[,1]) You may want to check Rnews volume 4(1) Regards Duncan Mackay At 09:50 3/09/2009, you wrote:>Hi all, > >I have the following problem: I have a csv-file consisting of timestamp >values (no dates), e.g.: >Timestamp1;Timestamp2; >05:24:43;05:25:05; >15:47:02;15:47:22; >18:36:05;18:36:24; >15:21:24;15:22:04; > >I need a vector with the difference of the two timestamps, so I read the >data with the read.csv-function: >myObj <- read.csv("file.csv",header=TRUE,sep=";"). > >I have then tried unsuccessfully to convert the data to time format (e.g. >using the strptime function like date1 <- >strptime(myObj[0],format="%H:%M:%S")) to be able to perform a datediff >operation. > >I would be very grateful if anybody could give me some assistance. > >Thanks in advance, >Ieyasu >-- >View this message in context: >http://www.nabble.com/diff-of-two-timestamps-tp25267603p25267603.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.
Try this: dat <- read.table(textConnection("Timestamp1 Timestamp2 05:24:43 05:25:05 15:47:02 15:47:22 18:36:05 18:36:24 15:21:24 15:22:04"), T) closeAllConnections() Reduce("-", lapply(dat, strptime, format = "%H:%M:%S")) On Wed, Sep 2, 2009 at 8:50 PM, sugo <ieyasu@sugimoto.at> wrote:> > Hi all, > > I have the following problem: I have a csv-file consisting of timestamp > values (no dates), e.g.: > Timestamp1;Timestamp2; > 05:24:43;05:25:05; > 15:47:02;15:47:22; > 18:36:05;18:36:24; > 15:21:24;15:22:04; > > I need a vector with the difference of the two timestamps, so I read the > data with the read.csv-function: > myObj <- read.csv("file.csv",header=TRUE,sep=";"). > > I have then tried unsuccessfully to convert the data to time format (e.g. > using the strptime function like date1 <- > strptime(myObj[0],format="%H:%M:%S")) to be able to perform a datediff > operation. > > I would be very grateful if anybody could give me some assistance. > > Thanks in advance, > Ieyasu > -- > View this message in context: > http://www.nabble.com/diff-of-two-timestamps-tp25267603p25267603.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of sugo > Sent: Wednesday, September 02, 2009 4:51 PM > To: r-help at r-project.org > Subject: [R] diff of two timestamps > > > Hi all, > > I have the following problem: I have a csv-file consisting of > timestamp > values (no dates), e.g.: > Timestamp1;Timestamp2; > 05:24:43;05:25:05; > 15:47:02;15:47:22; > 18:36:05;18:36:24; > 15:21:24;15:22:04; > > I need a vector with the difference of the two timestamps, so > I read the > data with the read.csv-function: > myObj <- read.csv("file.csv",header=TRUE,sep=";"). > > I have then tried unsuccessfully to convert the data to time > format (e.g. > using the strptime function like date1 <- > strptime(myObj[0],format="%H:%M:%S")) to be able to perform a datediff > operation.R indices start at 1, not 0, and you should use myObj[,1] to refer to the first column (myObj[1] means a data.frame containing one column, myObj[,1] means the first column itself). myObj$Timestamp1 and myObj[,"Timestamp1"] give that column as well. Hence if you want to use numerical indices try > strptime(myObj[,1],format="%H:%M:%S") [1] "2009-09-02 05:24:43" "2009-09-02 15:47:02" "2009-09-02 18:36:05" [4] "2009-09-02 15:21:24" but replacing the myObj[,1] with myObj$Timestamp1 will make things clearer. with() lets you save some typing by eliminating the repeated mObj$'s. > with(myObj, strptime(Timestamp2,format="%H:%M:%S") - + strptime(Timestamp1,format="%H:%M:%S")) Time differences in secs [1] 22 20 19 40 attr(,"tzone") [1] "" Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com> > I would be very grateful if anybody could give me some assistance. > > Thanks in advance, > Ieyasu > -- > View this message in context: > http://www.nabble.com/diff-of-two-timestamps-tp25267603p25267603.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. >