vincent.duval at boehringer-ingelheim.com
2007-Jun-20 07:23 UTC
[R] Computing time differences
Dear R users, I have a problem computing time differences using R. I have a date that are given using the following format: 20080620.00, where the 4 first digits represent the year, the next 2 ones the month and the last 2 ones the day. I would need to compute time differences between two vectors of this given format. I tried around trying to change this format into any type of time serie without any succes. Could some one provide me with some useful suggestion and/or tip to know where to look? I am using R-2.4.0 under Windows XP Thanks for your help, Vincent [[alternative HTML version deleted]]
Try this and see the help desk article in R News 4/1:> x <- 20080620.00 > x2 <- 20090218.00 > num2Date <- function(x) as.Date(paste(x), "%Y%m%d") > num2Date(x2) - num2Date(x)Time difference of 243 days> as.numeric(num2Date(x2) - num2Date(x))[1] 243 On 6/20/07, vincent.duval at boehringer-ingelheim.com <vincent.duval at boehringer-ingelheim.com> wrote:> Dear R users, > > I have a problem computing time differences using R. > > I have a date that are given using the following format: 20080620.00, where > the 4 first digits represent the year, the next 2 ones the month and the last > 2 ones the day. I would need to compute time differences between two vectors > of this given format. > > I tried around trying to change this format into any type of time serie > without any succes. > > Could some one provide me with some useful suggestion and/or tip to know > where to look? > > I am using R-2.4.0 under Windows XP > > Thanks for your help, > > Vincent > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Here is one way:
Vector1 <- c("20080621.00","20080623.00")
Vector2 <- c("20080620.00","20080622.00")
do.call(difftime,
c(apply(cbind(time1=Vector1,time2=Vector2),2,
function(x) strptime(x,format="%Y%m%d.00")),
units="hours"))
see ?strptime, ?difftime and
http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf
--- vincent.duval at boehringer-ingelheim.com wrote:
> Dear R users,
>
> I have a problem computing time differences using R.
>
> I have a date that are given using the following format: 20080620.00, where
> the 4 first digits represent the year, the next 2 ones the month and the
> last
> 2 ones the day. I would need to compute time differences between two
> vectors
> of this given format.
>
> I tried around trying to change this format into any type of time serie
> without any succes.
>
> Could some one provide me with some useful suggestion and/or tip to know
> where to look?
>
> I am using R-2.4.0 under Windows XP
>
> Thanks for your help,
>
> Vincent
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.