Dimitri Liakhovitski
2011-May-19 14:51 UTC
[R] Creating a "shifted" month (one that starts not on the first of each month but on another date)
Hello!
I have a data frame with dates. I need to create a new "month" that
starts on the 20th of each month - because I'll need to aggregate my
data later by that "shifted" month.
I wrote the code below and it works. However, I was wondering if there
is some ready-made function in some package - that makes it
easier/more elegant?
Thanks a lot!
# Example data:
mydf<-data.frame(mydate=seq(as.Date("2011-01-01"), length = 92, by
= "day"))
(mydf)
### Creating a new variable that has one value before
### the 20th of each month and next value after it
mydf$daynum<-as.numeric(format(mydate,"%d"))
library(zoo)
mydf$yearmon<-as.yearmon(mydf$mydate)
(mydf); str(mydf)
mydf$newfactor<-NA
for(i in unique(mydf$yearmon)){ # looping through "yearmon" (important
because true data has many years of data)
tempdf<-mydf[mydf$yearmon == i,]
which.month<-which(unique(mydf$yearmon)==i)
tempdf$newfactor[tempdf$daynum<20]<-which.month
tempdf$newfactor[tempdf$daynum>19]<-(which.month+1)
mydf[mydf$yearmon == i,]<-tempdf
}
(mydf)
--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com
jim holtman
2011-May-19 16:11 UTC
[R] Creating a "shifted" month (one that starts not on the first of each month but on another date)
try this:
mydf<-data.frame(mydate=seq(as.Date("2011-01-01"), length = 92, by
= "day"))
# add 'day' to the dataframe
mydf$day <- format(mydf$mydate, "%d")
mydf$newfactor <- cumsum(mydf$day == '20')
mydf
On Thu, May 19, 2011 at 10:51 AM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:> Hello!
> I have a data frame with dates. I need to create a new "month"
that
> starts on the 20th of each month - because I'll need to aggregate my
> data later by that "shifted" month.
> I wrote the code below and it works. However, I was wondering if there
> is some ready-made function in some package - that makes it
> easier/more elegant?
> Thanks a lot!
>
> # Example data:
> mydf<-data.frame(mydate=seq(as.Date("2011-01-01"), length =
92, by = "day"))
> (mydf)
>
> ### Creating a new variable that has one value before
> ### the 20th of each month and next value after it
>
> mydf$daynum<-as.numeric(format(mydate,"%d"))
> library(zoo)
> mydf$yearmon<-as.yearmon(mydf$mydate)
> (mydf); str(mydf)
>
> mydf$newfactor<-NA
> for(i in unique(mydf$yearmon)){ # looping through "yearmon"
(important
> because true data has many years of data)
> ? ? ? ?tempdf<-mydf[mydf$yearmon == i,]
> ? ? ? ?which.month<-which(unique(mydf$yearmon)==i)
> ? ? ? ?tempdf$newfactor[tempdf$daynum<20]<-which.month
> ? ? ? ?tempdf$newfactor[tempdf$daynum>19]<-(which.month+1)
> ? ? ? ?mydf[mydf$yearmon == i,]<-tempdf
> }
> (mydf)
>
> --
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?