-2

I am working on a function to help aggregate time series data, some of which needs to be weighted. This function should be capable of taking interval level data and aggregating to daily, weekly, monthly, quarterly, or yearly levels. The code below will, when removed from the function, generate the expected output, an xts object with the appropriate calculations, but the function will not. I have created the function below (with the print calls added to help my trouble shooting):

agg_cc_xts = function(x, aht, sl, asa, handled, rcvd, period = c("daily",
                                                                 "weekly",
                                                                 "monthly",
                                                                 "quarterly",
                                                                 "yearly")){

        x$wkld = x[,handled] * x[,aht]
        x$sl_thresh = x[,rcvd] * x[,sl]
        x$asa_calc = x[,handled] * x[,asa]       
        if(period == "daily"){ 
                a = apply.daily(x[,rcvd], sum, na.rm = TRUE)
                b = apply.daily(x[,handled], sum, na.rm = TRUE)
                c = apply.daily(x$wkld, sum, na.rm = TRUE)
                d = apply.daily(x$sl_thresh, sum, na.rm = TRUE)
                e = apply.daily(x$asa_calc, sum, na.rm = TRUE)
        } else {
                if(period == "monthly"){
                        a = apply.monthly(x[,rcvd], sum, na.rm = TRUE)
                        b = apply.monthly(x[,handled], sum, na.rm = TRUE)
                        c = apply.monthly(x$wkld, sum, na.rm = TRUE)
                        d = apply.monthly(x$sl_thresh, sum, na.rm = TRUE)
                        e = apply.monthly(x$asa_calc, sum, na.rm = TRUE)
                } else {
                        if(period == "quarterly"){
                                a = apply.quarterly(x[,rcvd], sum, na.rm = TRUE)
                                b = apply.quarterly(x[,handled], sum, na.rm = TRUE)
                                c = apply.quarterly(x$wkld, sum, na.rm = TRUE)
                                d = apply.quarterly(x$sl_thresh, sum, na.rm = TRUE)
                                e = apply.quarterly(x$asa_calc, sum, na.rm = TRUE)
                        } else {
                                if(period == "yearly"){
                                        a = apply.yearly(x[,rcvd], sum, na.rm = TRUE)
                                        b = apply.yearly(x[,handled], sum, na.rm = TRUE)
                                        c = apply.yearly(x$wkld, sum, na.rm = TRUE)
                                        d = apply.yearly(x$sl_thresh, sum, na.rm = TRUE)
                                        e = apply.yearly(x$asa_calc, sum, na.rm = TRUE)
                                } else {
                                        if(period == "weekly"){
                                                a = apply.weekly(x[,rcvd], sum, na.rm = TRUE)
                                                b = apply.weekly(x[,handled], sum, na.rm = TRUE)
                                                c = apply.weekly(x$wkld, sum, na.rm = TRUE)
                                                d = apply.weekly(x$sl_thresh, sum, na.rm = TRUE)
                                                e = apply.weekly(x$asa_calc, sum, na.rm = TRUE)
                                        }
                                }
                        }
                }
        }
        print(paste(length(a), class(a), length(.index(a))))
        print(paste(length(b), class(b), length(.index(b))))
        print(paste(length(c), class(c), length(.index(c))))
        print(paste(length(d), class(d), length(.index(d))))
        print(paste(length(e), class(e), length(.index(e))))
        print(head(a))
        print(head(b))
        print(head(c))
        print(head(d))
        print(head(e))
        y = cbind.xts(a, b, c, d, e)
        y$aht = y$wkld/y$handled
        y$sl = y$sl_thresh/y$rcvd
        y$asa = y$asa_calc/y$handled
        drops = c("wkld", "sl_thresh", "asa_calc")
        y = y[, !(names(y) %in% drops)]
        return(y)
}

When I run it on a created data set, I get the error:

Error in NextMethod(.Generic) : dims [product 274] do not match the length of object [0]

The print calls show that each column is of equal length, there are the same number of rownames (the indexing), and the objects are still zoo/xts.

I am running this on the sample data below:

library(xts)

set.seed(12822)
dates = seq(as.Date("2016-01-01"),
            as.Date("2016-09-30"), by="days")
RECEIVED = rnorm(n = length(dates), mean = 8000, sd = 650)
AHT = rnorm(n = length(dates), mean = 650, sd = 15)
HANDLED = RECEIVED - rnorm(n = length(dates), mean = 240, sd = 24)
SL = rnorm(n = length(dates), mean = .75, sd = .25/3.1)
ASA = rnorm(n = length(dates), mean = 46, sd = 13)
df = cbind(dates, RECEIVED, HANDLED, AHT, SL, ASA)
#make sure to use as.xts as the xts() call is used to make NEW xts objects
a.xts = as.xts(df[, -1], order.by = dates)

set.seed(2)
dates = seq(as.Date("2016-01-01"),
            as.Date("2016-09-30"), by="days")
RECEIVED = rnorm(n = length(dates), mean = 4500, sd = 300)
AHT = rnorm(n = length(dates), mean = 700, sd = 20)
HANDLED = RECEIVED - rnorm(n = length(dates), mean = 135, sd = 13.5)
SL = rnorm(n = length(dates), mean = .65, sd = .30/3.1)
ASA = rnorm(n = length(dates), mean = 60, sd = 17)
df = cbind(dates, RECEIVED, HANDLED, AHT, SL, ASA)

b.xts = as.xts(df[, -1], order.by = dates)

all = rbind(a.xts, b.xts)

The questions I believe are important to be answered are:

What is generating this object of length zero inside the function? Why does it only generate when running this code inside the function and not inside the console? How might I circumvent/prevent this behavior?

Richard Lusch
  • 1,050
  • 10
  • 19

1 Answers1

0

The problem in this question didn't involve the merge in cbind as the error codes had lead me to believe. Rather, it was a question of naming conventions within the different matrices inside the function. I had previously solved the problem of passing names of a data frame to the function with the assistance of the answer to the question below.

Pass a data.frame column name to a function

However, I did not recognize that the name of the data frame would flood over with it. In the code above I changed the lines:

    y$aht = y$wkld/y$handled
    y$sl = y$sl_thresh/y$rcvd
    y$asa = y$asa_calc/y$handled

To:

    y$aht = y$wkld/y[, handled]
    y$sl = y$sl_thresh/y[, rcvd]
    y$asa = y$asa_calc/y[, handled]

This overcame the error because the previous call of y$handled referenced an object that did not exist. This highlights the need to ensure consistent naming within a function in order for it to work properly.

Community
  • 1
  • 1
Richard Lusch
  • 1,050
  • 10
  • 19