抱歉,我是R.的新手
我有多个数据框product.43
,product.98
,product.103
,其中包含每个产品在某段时间内的销售情况,并有另一个变量index
,其中包含以下代码:每个产品:
products
43
98
103
如果我逐个做,那应该是这样的:
plot(product.43[,3],product.43[,2],type="o")
plot(product.98[,3],product.98[,2],type="o")
plot(product.103[,3],product.103[,2],type="o")
我想要做的是:
plot(product.i[,3],artigo_agreg.i[,2],type="o")
所以我想用i
替换index
中带有for
循环的产品代码。
答案 0 :(得分:2)
使用get()
。这是一个例子:
abc.1 <- 123
abc.2 <- 456
for(i in 1:2){
var <- paste('abc', i, sep='.')
x <- get(var)
print(x)
}
## [1] 123
## [1] 456
如您所见,您可以创建一个变量(var
),其中包含您要使用的变量的名称。然后,您可以使用其名称get()
变量的值。
在您的具体情况下,这将是这样的:
for(i in index) {
var <- paste('product', i, sep='.')
product.i <- get(var)
# Do whatever you need to do with product.i
}