[R] identifying weeks (dates) that certain days (dates) fall into

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Tue Aug 2 16:36:47 CEST 2011


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



More information about the R-help mailing list