I have 2 data series viz 'usagexts' and 'tempxts'. I want to create 3 subsets for each of the 2 objects. I am able to do that for each of the series individually looping over the dates parameters using variables for start and end periods. No problem there. However, I want to do the same for the 2 series looping over the names of the 2 series as well.
In other words, the xts object names also need to come from the variables.
Thsi is what I have tried - one with hardcoded series names with variable names for the start and end dates works but the other with variables names for the series names and for the start and end dates doesn't like so :
This code works :
varcount <- 2
usagextsobjects <- vector((varcount * nrow(sum_datesdf)), mode = "list")
for (i in 1:varcount)
{for (j in 1:nrow(sum_datesdf))
{if (i == 1)
{usagextsobjects[[(((i - 1) * nrow(sum_datesdf)) + j)]] <- usagexts[paste(sum_datesdf$startperioddate[j], sum_datesdf$endperioddate[j], sep = "/")]
}
else
{usagextsobjects[[(((i - 1) * nrow(sum_datesdf)) + j)]] <- tempxts[paste(sum_datesdf$startperioddate[j], sum_datesdf$endperioddate[j], sep = "/")]
}
}
}
This doesn't :
for (i in 1:varcount)
{for (j in 1:nrow(sum_datesdf))
{if (i == 1)
{modelformulae <- paste0(paste(paste0(sum_datesdf$var1[i], "xts[", sum_datesdf$startperioddate[j]), sum_datesdf$endperioddate[j], sep = "/"),"]")
}
else
{modelformulae <- paste0(paste(paste0(sum_datesdf$var2[i], "xts[", sum_datesdf$startperioddate[j]), sum_datesdf$endperioddate[j], sep = "/"),"]")
}
usagextsobjects[[(((i - 1) * nrow(sum_datesdf)) + j)]] <- as.formula(modelformulae)
}
}
The content of the variable modelformulae for i == 1 is as follows: usagexts[2015-10-01/2016-03-31]
. But this doesn't translate to creating the relevant xts object
The sum_datesdf data frame is like this :
startperioddate,endperioddate,checkval,var1,var1_1,var2,var2_1,varname1,varname1_1,varname2,varname2_1
1,2015-10-01,2016-03-31,1,usage,dusage,temp,dtemp,usage_1510_1603,dusage_1510_1603,temp_1510_1603,dtemp_1510_1603
6,2016-10-01,2017-03-31,1,usage,dusage,temp,dtemp,usage_1610_1703,dusage_1610_1703,temp_1610_1703,dtemp_1610_1703
11,2017-10-01,2018-03-31,1,usage,dusage,temp,dtemp,usage_1710_1803,dusage_1710_1803,temp_1710_1803,dtemp_1710_1803
I was expecting the object 'usagextsobjects' to contain a list of 6 xts objects. Using the first code, it does but using the second code it doesn't.
The error says this:
Error in
[.xts
(usagexts, 2015 - 10 - 1/2016 - 3 - 31) : subscript out of bounds