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.