R: beginning and end of month

date.end.month <- seq(as.Date("2012-02-01"),length=4,by="months")-1
firstDayMonth=function(x)
## —————————       ##
## Function takes a vector of  ##
## dates as its input.                 ##
##                                                  ##
## It produces a vector of        ##
## dates that are the first         ##
## in their respective months ##
## —————————       ##
{
  x=as.Date(as.character(x))
  day = format(x,format=”%d”)
  monthYr = format(x,format=”%Y-%m”)
  y = tapply(day,monthYr, min)
  first=as.Date(paste(row.names(y),y,sep=”-“))
  as.factor(first)
}
RStudio-icon