0

这是我希望该函数执行的操作:

datalist <- c("var1","var2",...)
my.function <- function(datalist){
   n        <- length(dlist)
   varnames <- paste("data", dlist, sep = ".")
   for (...) {   # for each var in 'varnames'
        ...      # grab each variable from some specific online dataset;
        ...      # do some basic data manipulation for each variable   
   }
   ...           # return all the results
}

对我来说主要的困难是:(1)如何进行循环,以便可以正确地临时存储抓取的数据,以及(2)在完成循环后如何返回多个变量;

编辑:

循环可以在循环期间创建我想要的变量,比如 VAR1 和 VAR2,它们存储在“dlist”参数中,但我不能在函数中操作 VAR1 或 VAR2,函数中的 dlist[1] 或 dlist[2] 会只给我一个字符串,而不是变量本身。

提前致谢。

4

1 回答 1

0

I think I have solved the problem and make the function work as I expected.

As I described in the question, the main problem in fact is how to manipulate the variables while VAR1 and VAR2 themselves are strings in the function.

eval combined with as.name should work:

eval(as.name(dlist[i]))
于 2013-10-06T07:48:44.373 回答