[R] Constructing dummy variables for months for a time series object

Gabor Grothendieck ggrothendieck at gmail.com
Sat Apr 26 01:32:59 CEST 2008


First generate a factor and then calculate its model matrix:

month. <- factor(cycle(x), labels = month.abb)
model.matrix(~ month.)[,-1]

Actually you may not even need the dummy variables at all if your
purpose is to do a regression.  You can use the factor
directly:

lm(x ~ month.)

By the way, be sure to check out ?monthplot


On Fri, Apr 25, 2008 at 3:40 AM, Megh Dal <megh700004 at yahoo.com> wrote:
> I have a TS of monthly observations.
>
>  head(data4)
>  1991(1)  1991(2)  1991(3)  1991(4)  1991(5)  1991(6)
> 12.00864 11.94203 11.98386 12.01900 12.19226 12.15488
>
>  Now I want to make 11 dummy variables indicating months. Therefore I did followings :
>
>  For Jan :
>  rep(c(rep(0,0), 1, rep(0, 11)), 17)
>
>  For Feb :
>  rep(c(rep(0,1), 1, rep(0, 10)), 17)
>
>  ........ and so on
>
>  But my question is there any way to aumate this? Or I have to do the above thing for all 11 months?
>
>
> ---------------------------------
> [[elided Yahoo spam]]
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list