[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-01 16:48 UTC
[R] time segments intersection
Hi all, Please, how could I calculate the time that two time segments has in common? Is there any function to perform this calculation? For instance, given four POSIXlt objects... endPeriod<-as.POSIXlt("2008-09-30") startPeriod<-as.POSIXlt("2007-10-01") endProject<-as.POSIXlt("2007-05-31") startProject<-as.POSIXlt("2006-12-01") that limit two time segments... project <- as.numeric(endProject-startProject) period <- as.numeric(endPeriod-startPeriod) How could I calculate the time project and period overlap? Thanks for your help! Ricardo -- Ricardo Rodr?guez Your XEN ICT Team
help("%in%") may be of use. However I don't see any overlap in your example. --- On Wed, 10/1/08, [Ricardo Rodriguez] Your XEN ICT Team <webmaster at xen.net> wrote:> From: [Ricardo Rodriguez] Your XEN ICT Team <webmaster at xen.net> > Subject: [R] time segments intersection > To: r-help at r-project.org > Cc: "Antonio Martinez Cortizas" <antonio.martinez.cortizas at usc.es> > Received: Wednesday, October 1, 2008, 12:48 PM > Hi all, > > Please, how could I calculate the time that two time > segments has in > common? Is there any function to perform this calculation? > > For instance, given four POSIXlt objects... > > endPeriod<-as.POSIXlt("2008-09-30") > startPeriod<-as.POSIXlt("2007-10-01") > endProject<-as.POSIXlt("2007-05-31") > startProject<-as.POSIXlt("2006-12-01") > > that limit two time segments... > > project <- as.numeric(endProject-startProject) > period <- as.numeric(endPeriod-startPeriod) > > How could I calculate the time project and period overlap? > > Thanks for your help! > > Ricardo > > > > -- > Ricardo Rodr?guez > Your XEN ICT Team > > ______________________________________________ > 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.__________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com.
Try this:> library(zoo) > # create two time series to test with > z1 <- zooreg(0:10, Sys.Date()) > z2 <- lag(z1, 5) > > z12008-10-02 2008-10-03 2008-10-04 2008-10-05 2008-10-06 2008-10-07 2008-10-08 2008-10-09 2008-10-10 2008-10-11 2008-10-12 0 1 2 3 4 5 6 7 8 9 10> z22008-09-27 2008-09-28 2008-09-29 2008-09-30 2008-10-01 2008-10-02 2008-10-03 2008-10-04 2008-10-05 2008-10-06 2008-10-07 0 1 2 3 4 5 6 7 8 9 10> > # intersect them > merge(z1, z2, all = FALSE)z1 z2 2008-10-02 0 5 2008-10-03 1 6 2008-10-04 2 7 2008-10-05 3 8 2008-10-06 4 9 2008-10-07 5 10> range(time(merge(z1, z2, all = FALSE)))[1] "2008-10-02" "2008-10-07" On Wed, Oct 1, 2008 at 12:48 PM, [Ricardo Rodriguez] Your XEN ICT Team <webmaster at xen.net> wrote:> Hi all, > > Please, how could I calculate the time that two time segments has in common? > Is there any function to perform this calculation? > > For instance, given four POSIXlt objects... > > endPeriod<-as.POSIXlt("2008-09-30") > startPeriod<-as.POSIXlt("2007-10-01") > endProject<-as.POSIXlt("2007-05-31") > startProject<-as.POSIXlt("2006-12-01") > > that limit two time segments... > > project <- as.numeric(endProject-startProject) > period <- as.numeric(endPeriod-startPeriod) > > How could I calculate the time project and period overlap? > > Thanks for your help! > > Ricardo > > > > -- > Ricardo Rodr?guez > Your XEN ICT Team > > ______________________________________________ > 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. >
[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-02 23:25 UTC
[R] time segments intersection
Hi Mark, markleeds at verizon.net wrote:> hi: I don't know if it's the best way but one way is to use an > POSIXct object. ( you can convert using as.POSIXct( > strptime(datestring)) because then each unit of digit is a second > and you can proceed from there. if you explain what you want to do > exactly, maybe i can help but i think as.POSIXct is easier because > it's in seconds essentially already.Thanks for your help. I only want to get the number of days, if any, the two time segments have in common. I do hope I am clear enough. Greetings, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team
[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-02 23:49 UTC
[R] time segments intersection
Not yet. I'll keep trying. Please, what format string must I pass to strptime to get the date as a number? Thanks. Cheers, Ricardo markleeds at verizon.net wrote:> did you get zoo to work and try gabor's solution because i wasn't > really following what you wanted. > > > > On Thu, Oct 2, 2008 at 7:25 PM, [Ricardo Rodriguez] Your XEN ICT Team > wrote: > >> Hi Mark, >> >> markleeds at verizon.net wrote: >>> hi: I don't know if it's the best way but one way is to use an >>> POSIXct object. ( you can convert using as.POSIXct( >>> strptime(datestring)) because then each unit of digit is a second >>> and you can proceed from there. if you explain what you want to do >>> exactly, maybe i can help but i think as.POSIXct is easier because >>> it's in seconds essentially already. >> >> Thanks for your help. I only want to get the number of days, if any, >> the two time segments have in common. I do hope I am clear enough. >> >> Greetings, >> >> Ricardo >> >> -- >> Ricardo Rodr?guez >> Your XEN ICT Team >-- Ricardo Rodr?guez Your XEN ICT Team
[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-03 07:25 UTC
[R] time segments intersection
Thanks Mark, markleeds at verizon.net wrote:> > > # MAKE POSIXct OBJECTS FROM CHARACTER STRINGS > temp1 <- as.POSIXct(strptime("2007-02-02","%Y-%m-%d")) > temp2 <- as.POSIXct(strptime("2007-02-01","%Y-%m-%d")) > > # TEMP1 AND TEMP2 ARE SORT OF NUMBERS BUT EACH UNIT IS ONE > # SECOND > > result <- difftime(temp1,temp2) > > # STILL NOT REALLY A NUMBER > print(result) > print(str(result)) > > # MAKE IT A NUMBER > numberresult <- unclass(result) > attributes(numberresult) <- NULL > print(numberresult)This does the trick: endPeriod<-as.POSIXct(strptime("2008-09-30","%Y-%m-%d")) startPeriod<-as.POSIXct(strptime("2007-10-01","%Y-%m-%d")) endProject<-as.POSIXct(strptime("2007-12-31","%Y-%m-%d")) startProject<-as.POSIXct(strptime("2006-12-01","%Y-%m-%d")) attributes(endPeriod) <- NULL attributes(startPeriod) <- NULL attributes(endProject) <- NULL attributes(startProject) <- NULL overlappingDays <- length(intersect(startPeriod:endPeriod,startProject:endProject))/86400 print(overlappingDays) [1] 91.04168 But as far as I see here, the method is extremely slow (some seconds for a single case). It solves the problem now, but taking into account that I must use the method for a quite large database, I think I must look for anything faster. Any new idea will be welcome!!! Cheers, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team
On Fri, Oct 3, 2008 at 2:48 AM, [Ricardo Rodriguez] Your XEN ICT Team <webmaster at xen.net> wrote:> Thanks Gabor, > > Gabor Grothendieck wrote: >> >> Try: >> >> chooseCRANmirror() > > It works the call to the pop-up window, but it fails now whatever mirror I > use with the following message: > >> install.packages("zoo",repos=chooseCRANmirror())Its as follows: chooseCRANmirror() install.packages("zoo", dep = TRUE) or maybe: chooseCRANmirror() install.packages("zoo", dep = TRUE, type = "mac.binary") If those do not work try installing some other packages, e.g. chron, to see if you can install anything. If you still have problems there is something wrong with your installation. Ask on the r-sig-mac list.> /bin/sh: tar: command not found > Error in sprintf(gettext(fmt, domain = domain), ...) : > argument is missing, with no default >> > > Does this make sense for you? Thanks! > > Cheers, > > Ricardo > > -- > Ricardo Rodr?guez > Your XEN ICT Team > >
> Its as follows: > > chooseCRANmirror() > install.packages("zoo", dep = TRUE) > > or maybe: > > chooseCRANmirror() > install.packages("zoo", dep = TRUE, type = "mac.binary") > > If those do not work try installing some other packages, e.g. chron, > to see if you can install anything. If you still have problems there > is something wrong with your installation. Ask on the r-sig-mac list.Also ensure that you have an up-to-date version of R. Hadley -- http://had.co.nz/
[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-03 20:53 UTC
[R] time segments intersection
Thanks Hadley, hadley wickham wrote:> > Also ensure that you have an up-to-date version of R. > > Hadley > >I think the most recent release... > version _ platform i386-apple-darwin8.11.1 arch i386 os darwin8.11.1 system i386, darwin8.11.1 status major 2 minor 7.2 year 2008 month 08 day 25 svn rev 46428 language R version.string R version 2.7.2 (2008-08-25) -- Ricardo Rodr?guez Your XEN ICT Team
[Ricardo Rodriguez] Your XEN ICT Team
2008-Oct-03 20:53 UTC
[R] time segments intersection
Thanks Mark! Mark Leeds wrote:> Hi Ricardo: My date knowledge is mostly restricted to as.POSIXct so I don't > know what else to tell you. Did you ever work out that zoo downloading > problem because Gabor's solutions are usually excellent.I've not worked out the problems I am facing while installing zoo! Something is going wrong here, but I don't know what yet. I will keep this thread posted with the results or any other solutions I could find. Thanks for your help, Ricardo -- Ricardo Rodr?guez Your XEN ICT Team