Megh Dal
2008-Apr-25 07:40 UTC
[R] Constructing dummy variables for months for a time series object
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]]
N. Lapidus
2008-Apr-25 08:18 UTC
[R] Constructing dummy variables for months for a time series object
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080425/2252337c/attachment.pl
N. Lapidus
2008-Apr-25 08:19 UTC
[R] Constructing dummy variables for months for a time series object
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible Url : https://stat.ethz.ch/pipermail/r-help/attachments/20080425/0c457c73/attachment.pl
Bill.Venables at csiro.au
2008-Apr-25 23:09 UTC
[R] Constructing dummy variables for months for a time series object
The simplest way is probably to generate them as the columns of a matrix. Let's suppose you have data from Jan 1991 up to Mar 2008.> X <- outer(rep(month.abb, length = 16*12+3), month.abb, "==") + 0> ## the "+ 0" will convert logical to numeric> dim(X)[1] 195 12> dimnames(X)[[2]] <- month.abb > head(X)Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec [1,] 1 0 0 0 0 0 0 0 0 0 0 0 [2,] 0 1 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 1 0 0 0 0 0 0 0 0 0 [4,] 0 0 0 1 0 0 0 0 0 0 0 0 [5,] 0 0 0 0 1 0 0 0 0 0 0 0 [6,] 0 0 0 0 0 1 0 0 0 0 0 0>To get the July indicators, use X[, "Jul"], &c Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Megh Dal Sent: Friday, 25 April 2008 5:40 PM To: r-help at stat.math.ethz.ch Subject: [R] Constructing dummy variables for months for a time series object 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.
Gabor Grothendieck
2008-Apr-25 23:32 UTC
[R] Constructing dummy variables for months for a time series object
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. >