creating dummy variable for years

994 views Asked by At

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"))
2

There are 2 answers

2
Vedda On

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.

0
akrun On

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))