I have created some R code to create a data frame with a list of dates (1st of each month spanning from April 2013 to July 2022) in the 1st column and 12 additional columns labelled by each of the respective months. I want to put a 1 in the month column if the date is in that month and 0 if not.
I have managed to generate the below code which creates everything I want apart from the fact that column 13 - 'dec' returns all 0's and I cannot figure out why.
Would someone be able to advise why this is happening?
mc_mon <- seq(as.Date("2013/04/01"), as.Date("2022-07-01"), by="month")
mc_mon <- as.data.frame(mc_mon)
month_indicator_mat <- data.frame(mc_mon$mc_mon,
jan=vector(mode = "integer", length = nrow(mc_mon)),
feb=vector(mode = "integer", length = nrow(mc_mon)),
mar=vector(mode = "integer", length = nrow(mc_mon)),
apr=vector(mode = "integer", length = nrow(mc_mon)),
may=vector(mode = "integer", length = nrow(mc_mon)),
jun=vector(mode = "integer", length = nrow(mc_mon)),
jul=vector(mode = "integer", length = nrow(mc_mon)),
aug=vector(mode = "integer", length = nrow(mc_mon)),
sep=vector(mode = "integer", length = nrow(mc_mon)),
oct=vector(mode = "integer", length = nrow(mc_mon)),
nov=vector(mode = "integer", length = nrow(mc_mon)),
dec=vector(mode = "integer", length = nrow(mc_mon)))
for(i in seq_along(1:nrow(month_indicator_mat))){
for(j in seq_along(2:13)){
if(j==2){if(substr(month_indicator_mat[i,1],6,7) == "01") {month_indicator_mat[i,j] <- 1}}
else if(j==3){if(substr(month_indicator_mat[i,1],6,7) == "02") {month_indicator_mat[i,j] <- 1}}
else if(j==4){if(substr(month_indicator_mat[i,1],6,7) == "03") {month_indicator_mat[i,j] <- 1}}
else if(j==5){if(substr(month_indicator_mat[i,1],6,7) == "04") {month_indicator_mat[i,j] <- 1}}
else if(j==6){if(substr(month_indicator_mat[i,1],6,7) == "05") {month_indicator_mat[i,j] <- 1}}
else if(j==7){if(substr(month_indicator_mat[i,1],6,7) == "06") {month_indicator_mat[i,j] <- 1}}
else if(j==8){if(substr(month_indicator_mat[i,1],6,7) == "07") {month_indicator_mat[i,j] <- 1}}
else if(j==9){if(substr(month_indicator_mat[i,1],6,7) == "08") {month_indicator_mat[i,j] <- 1}}
else if(j==10){if(substr(month_indicator_mat[i,1],6,7) == "09") {month_indicator_mat[i,j] <- 1}}
else if(j==11){if(substr(month_indicator_mat[i,1],6,7) == "10") {month_indicator_mat[i,j] <- 1}}
else if(j==12){if(substr(month_indicator_mat[i,1],6,7) == "11") {month_indicator_mat[i,j] <- 1}}
else if(j==13){if(substr(month_indicator_mat[i,1],6,7) == "12") {month_indicator_mat[i,j] <- 1}} }}