为什么函数内部创建的对象长度为零,但不在外部?

时间:2016-10-19 16:52:22

标签: r xts

我正在开发一个函数来帮助汇总时间序列数据,其中一些需要加权。此功能应能够获取间隔级别数据并汇总到每日,每周,每月,每季度或每年级别。当从函数中删除时,下面的代码将生成预期的输出,具有适当计算的xts对象,但该函数不会。我创建了下面的功能(添加了打印调用以帮助我解决问题):

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)
}

当我在创建的数据集上运行它时,我收到错误:

NextMethod(.Generic)中的错误:   dims [product 274]与object [0]

的长度不匹配

打印调用显示每列的长度相等,有相同数量的rownames(索引),对象仍然是zoo / xts。

我在以下示例数据上运行此操作:

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)

我认为重要的问题是:

在函数内生成这个长度为零的对象是什么? 为什么只在函数内部而不是在控制台内部运行此代码时生成? 我该如何规避/防止这种行为?

1 个答案:

答案 0 :(得分:0)

这个问题中的问题并没有涉及cbind中的合并,因为错误代码让我相信。相反,它是在函数内部的不同矩阵内命名约定的问题。我之前已经解决了在下面问题的答案的帮助下将数据框的名称传递给函数的问题。

Pass a data.frame column name to a function

但是,我没有意识到数据框的名称会随之泛滥。在上面的代码中我更改了行:

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

要:

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

这克服了错误,因为之前调用的y $ handle引用了一个不存在的对象。这突出了需要确保函数内的一致命名,以使其正常工作。