[变量R的更改名称

问题描述 投票:-3回答:1

我正在尝试创建要使用的变量,对于每个变量,M000M001M002M003

示例

c.n_vars <- nrow(comb)
for (i in 1:c.n_vars)
{
paste("M",comb[i,1],comb[i,2],comb[i,3]) = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

梳子是所有组合的地方

a <- c(0,1,2,3,4)
b <- c(0,1,2,3,4)
c <- c(0,1,2,3,4)
comb <- expand.grid(a, b,c)



row parameter1 parameter2 parameter3
1     0           0          0
2     1           0          0
3     2           0          0
4     3           0          0
5     4           0          0
6     0           1          0
7     1           1          0
8     2           1          0
9     3           1          0
10    4           1          0
11    0           2          0
12    1           2          0
13    2           2          0
14    3           2          0
15    4           2          0

并且Arima是

arima <- data.frame(c(2,1,4))

row parameters
1       2
2       1
3       4

我正在尝试创建

c.n_vars <- nrow(comb)
for (i in 1:c.n_vars)
{
paste("M",comb[i,1],comb[i,2],comb[i,3]) = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

此代码必须返回

    for i = 1

    M000 = arima model saved in that variable

    for i = 2

    M100 = arima model saved in that variable

    for i = 3

    M200 = arima model saved in that variable
.
.
.
.
.
for i = 15

  M420 = arima model saved in that variable

并且出现以下错误

Error in paste("M", comb[i, 1], comb[i, 2], comb[i, 3]) = Arima(y, order = c(arima[1,  :  
     assignment target expands an object out of language

我需要将变量'i'的每次迭代保存在不同的变量中

有什么解决方法吗?或另一种方式]

r static
1个回答
0
投票

您的示例代码仍然不完整。我无法运行它。例如,缺少y。

正如Base_R_Best_R所指出的,您不能使用粘贴来创建这样的变量。您可以改用以下模式。还要注意,我用paste0()替换了paste(),以避免名称中出现空格:

result = list()
for (i in 1:c.n_vars)
{
  result[[paste0("M",comb[i,1],comb[i,2],comb[i,3])]] = Arima(y,order=c(arima[1,1],arima[2,1],arima[3,1]),seasonal=list(order=c(comb[i,1],comb[i,2],comb[i,3]),period=12))
}

像这样访问变量:

result$M100
© www.soinside.com 2019 - 2024. All rights reserved.