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}} }}
CodePudding user response:
Try replace
numeric month number of a vector of zeros with 1
, might be less tedious.
mc_mon <- seq(as.Date("2013/04/01"), as.Date("2022-07-01"), by="month")
res <- data.frame(mc_mon, `colnames<-`(t(sapply(as.numeric(substr(mc_mon, 6, 7)), \(x)
replace(numeric(12L), x, 1))), month.abb))
res
# mc_mon Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 1 2013-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 2 2013-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 3 2013-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 4 2013-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 5 2013-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 6 2013-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 7 2013-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 8 2013-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 9 2013-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 10 2014-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 11 2014-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 12 2014-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 13 2014-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 14 2014-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 15 2014-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 16 2014-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 17 2014-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 18 2014-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 19 2014-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 20 2014-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 21 2014-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 22 2015-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 23 2015-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 24 2015-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 25 2015-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 26 2015-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 27 2015-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 28 2015-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 29 2015-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 30 2015-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 31 2015-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 32 2015-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 33 2015-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 34 2016-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 35 2016-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 36 2016-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 37 2016-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 38 2016-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 39 2016-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 40 2016-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 41 2016-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 42 2016-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 43 2016-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 44 2016-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 45 2016-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 46 2017-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 47 2017-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 48 2017-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 49 2017-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 50 2017-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 51 2017-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 52 2017-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 53 2017-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 54 2017-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 55 2017-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 56 2017-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 57 2017-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 58 2018-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 59 2018-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 60 2018-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 61 2018-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 62 2018-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 63 2018-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 64 2018-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# 65 2018-08-01 0 0 0 0 0 0 0 1 0 0 0 0
# 66 2018-09-01 0 0 0 0 0 0 0 0 1 0 0 0
# 67 2018-10-01 0 0 0 0 0 0 0 0 0 1 0 0
# 68 2018-11-01 0 0 0 0 0 0 0 0 0 0 1 0
# 69 2018-12-01 0 0 0 0 0 0 0 0 0 0 0 1
# 70 2019-01-01 1 0 0 0 0 0 0 0 0 0 0 0
# 71 2019-02-01 0 1 0 0 0 0 0 0 0 0 0 0
# 72 2019-03-01 0 0 1 0 0 0 0 0 0 0 0 0
# 73 2019-04-01 0 0 0 1 0 0 0 0 0 0 0 0
# 74 2019-05-01 0 0 0 0 1 0 0 0 0 0 0 0
# 75 2019-06-01 0 0 0 0 0 1 0 0 0 0 0 0
# 76 2019-07-01 0 0 0 0 0 0 1 0 0 0 0 0
# [ reached 'max' / getOption("max.print") -- omitted 36 rows ]
Explanation:
as.numeric(substr(mc_mon, 6, 7))
extracts the month from the 6th to 7th substring and converts to numeric, thus getting the x-position where we want a1
replace(numeric(12L), x, 1)
replaces xth position of a vector out of zeroes with1
sapply
loops over the x-positions for each month and creates a
matrix (in contrast tolapply
which would create a list); the
result needs to bet
ransposedwe set
`colnames<-`
in one step using themonth.abb
stored in R as a constant (we could dotolower(month.abb))
to get lowercase)finally we combine
mc_mon
and thesapply
result in a
data.frame
.