How to call an object with the character variable of the same name

后端 未结 1 822
不思量自难忘°
不思量自难忘° 2020-11-22 05:28

I\'m trying to write a function in R to batch-analyse a number of files in a similar manner. The files are of class ExpressionSetIllumina. I can make a characte

相关标签:
1条回答
  • 2020-11-22 05:47

    I think you want get.

    data <- get(i)
    

    That said, once you start using get (and its counterpart, assign), you usually end up with horrible unreadable code.

    For batch analyses like yours, it is often better to read all your data into a list of data frames, then make liberal use of lapply. Something like:

    data_files <- list.files()
    all_vars <- lapply(data_files, function(file)
    {
      vars_loaded <- load(file)
      mget(vars_loaded, parent.frame())
    })
    

    mget is the version of get that retrieves multiple variables at once. Here it is used to retrieve all the things that were loaded by the call to load.

    Now you have a list of lists: the top level list is related to the file, lower level lists contain the variables loaded from that file.

    0 讨论(0)
提交回复
热议问题