Suggest you read the article in R News 4/1 on dates and times and try this:
library(zoo) library(chron) z <- zoo(101:200, seq(chron("1/1/2000"), length = 100)) zz <- z[with(month.day.year(time(z)), day >= 21 & month %% 2 == 1)] zz[!duplicated(as.yearmon(time(zz)))] On Nov 19, 2007 9:57 PM, Carles Fan <[EMAIL PROTECTED]> wrote: > many thanks Gabor. > > Q1: > if i want to do with the following 2 conditions, how can i do? > condition 1) every n-th day > condition 2) every x-month <----- x = 1 to 12 > > Eg: > condition 1) every 21-th day > condition 2) every 2-month > > 1st data: 2000-01-21 100 > 2nd data: 2000-03-21 101 > 3rd data: 2000-05-21 103 > ... > ... > ... > > Q2: > how can i loop thur 'z" from start to the end with the above conditions? > eg: > condition 1) every n-th day > condition 2) every 2-month > > 1st batch run > 1st data: 2000-01-01 100 > 2nd data: 2000-03-01 104 > 3rd data: 2000-05-01 106 > ... > ... > ... > > 2nd batch run > 1st data: 2000-01-02 103 > 2nd data: 2000-03-02 104 > 3rd data: 2000-05-02 106 > ... > ... > ... > > 3rd batch run > 1st data: 2000-01-03 103 > 2nd data: 2000-03-03 105 > 3rd data: 2000-05-03 106 > ... > ... > ... > > > > > On Nov 16, 2007 2:53 AM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > > > On Nov 15, 2007 12:54 PM, Carles Fan <[EMAIL PROTECTED]> wrote: > > > Dear all > > > > > > i have a time series containing trading dates and historical stock prices: > > > Date Price > > > 10-Jan-2007 100 > > > 11-Jan-2007 101 > > > 13-Jan-2007 99 > > > ...... > > > ...... > > > ...... > > > 10-Nov-2007 200 > > > > > > i want to sample every 21st data of each month: > > > 21-Jan-2007 101 > > > 21-Feb-2007 111 > > > 21-Mar-2007 131 > > > ...... > > > ...... > > > ...... > > > 21-Oct-2007 140 > > > > > > 1) how can i do that? > > > 2) if some of the dates are non-trading day, how can i tell "R" to use > > > "modified following" or "following" data? > > > > > > Using zoo, z is some test data. zz is only those points whose day > > of the month is 21 or more. In the last line we keep only the first > > point in each month in zz. > > > > library(zoo) > > z <- zoo(101:200, as.Date("2000-01-01") + seq(0, len = 100, by = 2)) > > > > zz <- z[as.numeric(format(time(z), "%d")) >= 21] > > zz[!duplicated(as.yearmon(time(zz)))] > > > ______________________________________________ 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.