0

I have a data with year variable 1950-2007, however, each year is repeated 12 times because of the "month" variable in the other column. How can i create a year dummy variable? this is what i have and R is not accepting it

yeardummy <- cut(research$year, br=c(0, 1950:2007), labels=c("1950:2007"))
Ataboy Josef
  • 2,087
  • 3
  • 22
  • 27
nnyuss01
  • 9
  • 1

2 Answers2

0

Here is a function I use for creating multiple dummy variables for each year in my data set.

dummyCreator <- function(invec, prefix = NULL) {
     L <- length(invec)
     ColNames <- sort(unique(invec))
     M <- matrix(0L, ncol = length(ColNames), nrow = L,
                 dimnames = list(NULL, ColNames))
     M[cbind(seq_len(L), match(invec, ColNames))] <- 1L
     if (!is.null(prefix)) colnames(M) <- paste(prefix, colnames(M), sep = "_")
     M
} 

#Usage
dummy <- dummyCreator(research$year, prefix = "year")
research <- cbind(research, dummy)

Just pass variable from your dataset to it, and any prefix you want and it'll run through, then cbind to original dataset and you're good to go.

Hope this helps.

Vedda
  • 7,066
  • 6
  • 42
  • 77
  • A nice thing to do is to [share the source of the functions you use](http://stackoverflow.com/questions/27141872/dummy-variable-for-each-year/27142399#27142399). – A5C1D2H2I1M1N2O1R2T1 Dec 01 '14 at 09:01
  • @AnandaMahto absolutely. I forgot who I got it from, so thanks for reminding me. It's a great function! – Vedda Dec 01 '14 at 09:03
0

I would also do if the expected dummy is similar to @Amstell's answer

researchNew <- cbind(research, 
       setNames(as.data.frame(model.matrix( ~ 0+factor(year),
        data=research['year'])),paste('year', unique(research$year), sep="_")))

data

research <- data.frame(year=rep(c(1957:1958), each=12), month=rep(month.abb,2))
akrun
  • 874,273
  • 37
  • 540
  • 662