Skip to content

Looking for the first observation within the month

6 messages · Albert Pang, jim holtman, 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.

Lines <- "Date                    Observation

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
"

library(zoo)

# z <- read.zoo("myfile.dat", header = TRUE)
z <- read.zoo(textConnection(Lines), header = TRUE)

head1 <- function(x, n = 1) head(x, n)
aggregate(z, ave(time(z), as.yearmon(time(z)), FUN = head1), head1)


For more on zoo try:

library(zoo)
vignette("zoo")

and also read the Help Desk article in R News 4/1 about dates.
On 5/27/07, Albert Pang <albert.pang at mac.com> wrote:
#
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 pointer!

Albert
On May 27, 2007, at 10:48 PM, Gabor Grothendieck wrote:

            
#
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 convert it back again.  Then
we use window to select those dates.

window(z, as.Date(tapply(time(z), as.yearmon(time(z)), head, 1)))
On 5/27/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
#
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 <ggrothendieck at gmail.com> wrote: