[R] Looking for the first observation within the month

2007-05-27 Thread Albert Pang
Hi all, I have a simple data frame, first list is a list of dates (in "%Y-%m-%d" format) and second list an observation on that particular date. There might not be observations everyday. Let's just say there are no observations on saturdays and sundays. Now I want to select the first obs

Re: [R] Looking for the first observation within the month

2007-05-27 Thread jim holtman
Here is one way of doing it: > x <- "DateObservation + 2007-05-23 20 + 2007-05-22 30 + 2007-05-21 10 + 2007-04-10 50 + 2007-04-09 40 + 2007-04-07 30 + 2007-03-05 10" > x <- read.table(tex

Re: [R] Looking for the first observation within the month

2007-05-27 Thread Gabor Grothendieck
Use the zoo package to represent data like this. Here time(z) is a vector of the dates and as.yearmon(time(z)) is the year/month of each date. With FUN=head1, ave picks out the first date in any month and aggregate then aggregates over all values in the same year/month choosing the first one. Li

Re: [R] Looking for the first observation within the month

2007-05-27 Thread Albert Pang
I have only just able to dissect Jim's solution and realize I am actually not very far away from the answer. One last step was to use "lapply". Jim, thanks again for the help. Gabor, thanks for the suggestion. Let me have a read on what the zoo package is about. Thanks a lot for the poin

Re: [R] Looking for the first observation within the month

2007-05-27 Thread Gabor Grothendieck
One additional simplification. If we use simplify = FALSE then tapply won't simplify its answer to numeric and we can avoid using as.Date in the last solution: window(z, tapply(time(z), as.yearmon(time(z)), head, 1, simplify = FALSE)) On 5/27/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: >

Re: [R] Looking for the first observation within the month

2007-05-27 Thread Gabor Grothendieck
Here is one additional solution, also using zoo. Using z from the prior solution as.yearmon(time(z)) is, as before, the year/month of each date and tapply(time(z), as.yearmon(time(z)), head, 1) gets the first date within each month; however, tapply converts it to numeric so we use as.Date to conve