0
votes

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

1
What are you attempting in second block? Both xts objects, usagexts and tempxts, are being looped in first block.Parfait
I was trying to create a command which I could execute giving me an xts object with data from 01O/Oct15 to 31/Mar/16 and so on. Like the example shown where for i = 1 and j = 1, the string output is "usagexts[2015-10-01/2016-03-31]". The intent was this could be executed to give me the desired time series object. The contents of "sum_datesdf$var1[1]" is "usage."Deepak Agarwal

1 Answers

0
votes

Normally, to reference a named object by string, you need to use get() and not simply the string itself which you attempt with paste calls. And you are not building a formula object from string requiring, as.formula so remove that call. See adjustment below:

for (i in 1:varcount)
    {for (j in 1:nrow(sum_datesdf))
        {if (i == 1)
            { data <- get(sum_datesdf$var1[i])[paste(sum_datesdf$startperioddate[j], sum_datesdf$endperioddate[j], sep = "/")] 
            }
         else
            { data <- get(sum_datesdf$var2[i])[paste(sum_datesdf$startperioddate[j], sum_datesdf$endperioddate[j], sep = "/")]
            }
         usagextsobjects[[(((i - 1) * nrow(sum_datesdf)) + j)]] <- data
        }
    }

However, consider using Map (wrapper to mapply) to iterate elementwise through your sum_datesdf columns and build needed list rather than initialize and assign through nested for loops. Also, split into two Map calls to be binded together with c() for a single list:

get_data <- function(obj, start, end) {
   get(obj)[paste(start, end, sep = "/")]
}

usagextsobjects <- with(sum_datesdf,
                            c(Map(get_data, var1, startperioddate, endperioddate),
                              Map(get_data, var2, startperioddate, endperioddate)
                             )
                       )