Dimitri Liakhovitski
2011-Aug-02 14:36 UTC
[R] identifying weeks (dates) that certain days (dates) fall into
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
David Winsemius
2011-Aug-02 15:08 UTC
[R] identifying weeks (dates) that certain days (dates) fall into
The findInterval function should surely be tried in some form or another. On Aug 2, 2011, at 10:36 AM, Dimitri Liakhovitski 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.> findInterval(july4, weekly$week) [1] 14 66 # works "out of the box" Provides an index you cna use with weekly$week> 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?-- David Winsemius, MD West Hartford, CT
Gabor Grothendieck
2011-Aug-02 15:42 UTC
[R] identifying weeks (dates) that certain days (dates) fall into
On Tue, Aug 2, 2011 at 10:36 AM, Dimitri Liakhovitski <dimitri.liakhovitski at 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!This gives myrows: as.numeric(july4 - weekly[1,1]) %/% 7 + 1 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Dennis Murphy
2011-Aug-02 16:34 UTC
[R] identifying weeks (dates) that certain days (dates) fall into
Hi: You could try the lubridate package: library(lubridate) week(weekly$week) week(july4) [1] 27 27> weekfunction (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.liakhovitski at 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 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. >
Apparently Analagous Threads
- Identifying US holidays
- Looping through rows of all elements of a list that has variable length
- standardizing one variable by dividing each value by the mean - but within levels of a factor
- expand.grid on contents of a list
- "rounding" to a number that is LOWER than my number