You are getting 105 because the default behavior of findInterval is such that v[N+1] := + Inf (as noted in ? findInterval); that is, the last interval is actually taken to stretch from the final element of sbwl.dates unto eternity. It shouldn't be hard to write a catch line to set that to whatever you want for it to go to.
E.g., myFindInterval <- function(x, vec, rightmost.closed = FALSE, all.inside = FALSE) { FI = findInterval(x,vec,rightmost.closed, all.inside) FI[FI==length(vec)] <- 0 # or Inf or whatever return(FI) } Michael Weylandt On Thu, Aug 4, 2011 at 9:26 AM, Dimitri Liakhovitski < dimitri.liakhovit...@gmail.com> wrote: > Sorry for renewing the topoic. I thought it worked but now I've run > into a little problem: > > # My data frame with dates for week starts (Mondays) > y<-data.frame(week=seq(as.Date("2009-12-28"), > as.Date("2011-12-26"),by="week") ) > > # I have a vector of super bowl dates (including the future one for 2012): > > sbwl.dates<-as.Date(c("2005-02-06","2006-02-05","2007-02-04","2008-02-03","2009-02-01","2010-02-07","2011-02-06","2012-02-05")) > I want to find the weeks in y that contain super bowl dates for > applicable years. I am trying: > sbwl.weeks<-findInterval(sbwl.dates, y$week) > sbwl.weeks<-sbwl.weeks[sbwl.weeks>0] > (sbwl.weeks) > > 6 58 105 > y$flag<-0 > y$flag[sbwl.weeks]<-1 > > 6 and 58 are correct. But why am I getting 105 (the last row)? > Any way to fix it? > Thanks a lot! > Dimitri > > > > On Tue, Aug 2, 2011 at 12:57 PM, Dimitri Liakhovitski > <dimitri.liakhovit...@gmail.com> wrote: > > Thanks a lot, everyone! > > Dimitri > > > > On Tue, Aug 2, 2011 at 12:34 PM, Dennis Murphy <djmu...@gmail.com> > wrote: > >> Hi: > >> > >> You could try the lubridate package: > >> > >> library(lubridate) > >> week(weekly$week) > >> week(july4) > >> [1] 27 27 > >> > >>> week > >> function (x) > >> yday(x)%/%7 + 1 > >> <environment: namespace:lubridate> > >> > >> which is essentially Gabor's code :) > >> > >> HTH, > >> Dennis > >> > >> On Tue, Aug 2, 2011 at 7:36 AM, Dimitri Liakhovitski > >> <dimitri.liakhovit...@gmail.com> wrote: > >>> Hello! > >>> > >>> I have dates for the beginning of each week, e.g.: > >>> weekly<-data.frame(week=seq(as.Date("2010-04-01"), > >>> as.Date("2011-12-26"),by="week")) > >>> week # each week starts on a Monday > >>> > >>> I also have a vector of dates I am interested in, e.g.: > >>> july4<-as.Date(c("2010-07-04","2011-07-04")) > >>> > >>> I would like to flag the weeks in my weekly$week that contain those 2 > >>> individual dates. > >>> I can only think of a very clumsy way of doing it: > >>> > >>> myrows<-c(which(weekly$week==weekly$week[weekly$week>july4[1]][1]-7), > >>> which(weekly$week==weekly$week[weekly$week>july4[2]][1]-7)) > >>> weekly$flag<-0 > >>> weekly$flag[myrows]<-1 > >>> > >>> It's clumsy - because actually, my vector of dates of interest (july4 > >>> above) is much longer. > >>> Is there maybe a more elegant way of doing it? > >>> Thank you! > >>> -- > >>> Dimitri Liakhovitski > >>> marketfusionanalytics.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. > >>> > >> > > > > > > > > -- > > Dimitri Liakhovitski > > marketfusionanalytics.com > > > > > > -- > Dimitri Liakhovitski > marketfusionanalytics.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. > [[alternative HTML version deleted]] ______________________________________________ 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.