调用变量在循环外起作用,但在循环内不起作用

时间:2018-10-14 21:02:02

标签: r

我正在遍历vars_macro。 vars_macro中的第一个变量是c1372(下面的说明)。下面的代码运行良好:

len <- 32    
c1372[1:(len-z),1:1]

但是,当我尝试在下面的代码中调用同一变量(c1372)时,出现错误:

Error in m[1:(len - z), 1:1] : incorrect number of dimensions

代码:

output <- list()
forecast <- list()
    for(m in noquote(vars_macro)){

      output[[m]] <- list() # treat output as a list-of-lists 
      fit[[m]] <- list() # treat fit as a list-of-lists

      for(z in rev(1:6)) {

        x <- m[1:(len-z),1:1]
        x <- ts((x), start = c(2011, 4), frequency = 4)
        y <- Macro[1:(len-z),2:2]
        y <- ts((y), start = c(2011, 4), frequency = 4)
        t <- Macro[(len+1-z):(len+1-z),3:10]
        t <- ts((t), start = c(2019, 2), frequency = 4) 

        #fit model
        fit[[m]][[z]] <-auto.arima(y,xreg=x,seasonal=TRUE)
        output[[m]][[z]] <- forecast(fit[[m]][[z]],xreg=t)$mean

    }
    }

请注意,上面的代码在第一个变量(c1372)上失败,因此问题不在其他变量上。您只需写for(m in ("c1372"))

即可确认

投放:

dput(vars_macro)
c("c1372", "c5244", "c5640", "c6164", "b1372", "b5244", "b5640", 
"b6164", "v1372", "v5244", "v5640", "v6164", "bv1372", "bv5244", 
"bv5640", "bv6164")


dput(c1372)
structure(list(c1372 = c(1.386445329, 1.600103663, 1.906186443, 
1.962067415, 2.716663882, 1.875961101, 2.086589462, 2.115101307, 
2.960605275, 2.109288864, 2.730920081, 2.816577742, 4.006180002, 
3.503741762, 4.162132837, 4.122407811, 5.352681171, 3.961705849, 
4.773003078, 4.575654378, 5.71727247, 4.401603262, 5.204187541, 
4.7354794, 5.809822373, 4.137968937, 4.881120131, 4.812274313, 
6.143882981, 4.935116748, 5.95001413, 5.384694268)), row.names = c(NA, 
-32L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

OP中的代码失败,因为第一次执行行for(m in noquote(vars_macro))时,m被设置为单个元素字符向量c1372

因此,x <- m[1:(len-z),1:1]失败是因为m是单个元素字符向量,而不是具有32行和一列的数据帧。

在R中,一切都是对象,了解一个对象要操作的类型很重要。在字符向量和实际对象之间来回移动的机制是两个R函数get()assign()

assign()为对象分配名称。 get()检索给定名称的对象。

如果需要访问c1372数据帧而不是字符向量c1372,则需要使用get()函数来获取命名对象。

以OP中提供的数据为例:

vars_macro <- "c1372"
c1372 <- structure(list(c1372 = c(1.386445329, 1.600103663, 1.906186443, 
                                  1.962067415, 2.716663882, 1.875961101, 2.086589462, 2.115101307, 
                                  2.960605275, 2.109288864, 2.730920081, 2.816577742, 4.006180002, 
                                  3.503741762, 4.162132837, 4.122407811, 5.352681171, 3.961705849, 
                                  4.773003078, 4.575654378, 5.71727247, 4.401603262, 5.204187541, 
                                  4.7354794, 5.809822373, 4.137968937, 4.881120131, 4.812274313, 
                                  6.143882981, 4.935116748, 5.95001413, 5.384694268)), row.names = c(NA, 
                                                                                                     -32L), class = "data.frame")

len <- 32
theData <- NULL

for (m in vars_macro){
     theData <- get(m)
}
# print first few rows to show that get() worked
head(theData)

...以及输出:

> # print first few rows to show that get() worked
> head(theData)
     c1372
1 1.386445
2 1.600104
3 1.906186
4 1.962067
5 2.716664
6 1.875961
>