I have created below variables . Each one denotes a month . Eg. .arg1 refers to April , .arg2 May so on.
.arg1<-4
.arg2<-32
.arg3<-41
.arg4<-35
.arg5<-26
.arg6<-19
.arg7<-16
.arg8<-18
.arg9<-12
.arg10<-0
.arg11<-0
.arg12<-0
The sum of all variable is 203 . So my datarow is 203. I have created Month column.
I have Categorize Month name over there. Eg. .arg1 = 4 . That means Row 1 to Row 4 will have Value called 'Apr' in the Month column. The problem arises when I have value as Zero in between the .arg variables. On Execution of below script, by default Mar is getting created. Even though it has 0 value.
maxrows <- (.arg1 + .arg2+.arg3 + .arg4 + .arg5 + .arg6 + .arg7 + .arg8+ .arg9 + .arg10 + .arg11 + .arg12 )
m <- matrix(0, ncol = 1, nrow = maxrows)
m <- data.frame(m)
names(m)[1] <- 'Month'
m[1:.arg1,1] <- 'Apr'
m[(.arg1+1):(.arg2+.arg1),1] <- 'May'
m[(.arg2+.arg1+1 ):(.arg2+.arg1+.arg3),1] <- 'Jun'
m[(.arg2+.arg1+.arg3+1 ):(.arg2+.arg1+.arg3+.arg4),1] <- 'Jul'
m[(.arg2+.arg1+.arg3+.arg4+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5),1] <- 'Aug'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6),1] <- 'Sep'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7),1] <- 'Oct'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8),1] <- 'Nov'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+.arg9),1] <- 'Dec'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8 +.arg9+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+.arg9+.arg10),1] <- 'Jan'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8 +.arg9 +.arg10+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+.arg9+.arg10+.arg11),1] <- 'Feb'
m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8 +.arg9 +.arg10+.arg11+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+.arg9+.arg10+.arg11+.arg12),1] <- 'Mar'
Everything works fine with your code up until row m[(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+1 ):(.arg2+.arg1+.arg3+.arg4+.arg5+.arg6+.arg7+.arg8+.arg9),1] <- 'Dec'
. After that you start to overwrite the last value as the from:to
range remains the same because of the 0 frequency for months 'Jan'-'Mar'
.
The next code will overcome this issue (and much more simple):
month_name_count <- c(4, 32, 41, 35, 26, 19, 16, 18, 12, 0, 0, 0)
month_names <- c('Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar')
m <- data.frame('Month' = rep(month_names, month_name_count))