使用带有sqlQuery的r中的paste来定义for循环中的变量

时间:2022-05-01 21:29:25

I am trying to define a variable in a for loop in r using the paste function, an example is as follows:

我试图使用粘贴函数在r中的for循环中定义变量,示例如下:

for (i in 1:length(XX)) {
    (paste0("Test",Name[i],sep="")) <- sqlQuery(channel, paste("SampleQuery", sep = ""))
}

I get the following error:

我收到以下错误:

Error in (paste0("Test", Name[i], sep = "")) <- sqlQuery(channel,  : 
target of assignment expands to non-language object

Thanks for any help!

谢谢你的帮助!

1 个解决方案

#1


1  

From the documentation, the output of paste0 is a character vector, not a variable. You can't use a character vector and the <- operator to assign into a variable, for that you need assign().

从文档中,paste0的输出是一个字符向量,而不是一个变量。您不能使用字符向量和< - 运算符来分配变量,因为您需要assign()。

But anyway, you'd be better off storing a list of related sql results in an actual list, rather than a bunch of variables. Keeps the environment tidier and easier to access programmatically afterward

但无论如何,你最好将一个相关的sql结果列表存储在一个实际的列表中,而不是一堆变量。保持环境更整洁,更容易以编程方式访问

#1


1  

From the documentation, the output of paste0 is a character vector, not a variable. You can't use a character vector and the <- operator to assign into a variable, for that you need assign().

从文档中,paste0的输出是一个字符向量,而不是一个变量。您不能使用字符向量和< - 运算符来分配变量,因为您需要assign()。

But anyway, you'd be better off storing a list of related sql results in an actual list, rather than a bunch of variables. Keeps the environment tidier and easier to access programmatically afterward

但无论如何,你最好将一个相关的sql结果列表存储在一个实际的列表中,而不是一堆变量。保持环境更整洁,更容易以编程方式访问