Hi, Is there a function to get the last(or first) day of the week, given the week number of the year? For eg, week number for 7/20 is 29 as obtained by format(Sys.Date(),"%U"), is there a function which returns 7/25 - the last day of week # 29 TIA, Rao. [[alternative HTML version deleted]]
On Jul 20, 2010, at 6:37 PM, H Rao wrote:> Hi, > > Is there a function to get the last(or first) day of the week, given > the > week number of the year? > > For eg, week number for 7/20 is 29 as obtained by > format(Sys.Date(),"%U"), > is there a function which returns 7/25 - the last day of week # 29 >require(tis) nthMon <- function(x) as.Date(currentMonday(xTi=as.Date("2010-01-01")) +7*(x-1)) > nthMon(2) [1] "2010-01-11" Or: nthMonYr <- function(n, Yr) as.Date(currentMonday(xTi=as.Date(paste(Yr,"-01-01",sep="")))+7*(n-1)) > nthMonYr(2,2010) [1] "2010-01-11"> TIA, > Rao. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Tue, Jul 20, 2010 at 6:37 PM, H Rao <hydsdrao at gmail.com> wrote:> Hi, > > Is there a function to get the last(or first) day of the week, given the > week number of the year? > > For eg, week number for 7/20 is 29 as obtained by format(Sys.Date(),"%U"), > is there a function which returns 7/25 - the last day of week # 29 >%U weeks end on Saturday, not Sunday, so week 29 ends on July 24th rather than July 25th. At any rate supposing that we want the first and last day of the week according to %U and assume that if the week starts or ends in the following year that we we want the first or last day of this year. First, define the input year and week. y <- 2010; w <- 29 # Then create a sequence of days for at least the entire year # and pick off the range of dates whose format indicates # the desired year and week days <- as.Date(paste(y, 1, 1, sep = "-")) + 0:365 range(days[sprintf("%d %02d", y, w) == format(days, "%Y %U")]) # "2010-07-18" "2010-07-24"
Apparently Analagous Threads
- transforming year.weeknumber into dates
- Pseudo-instruction that overwrites its input register
- Pseudo-instruction that overwrites its input register
- Compileing 2.2.3a ( and ealier) on HPUX11
- supsmu(periodic=TRUE) can crash R by reading before start of array (PR#9502)