简体   繁体   English

R:使字符串引用一个对象

[英]R: Make character string refer to an object

I have a large list of files ( file1 , file2 , file3 , etc.) and, for each analysis, I want to refer to two files from this list (eg function(file1,file2) ).我有一个很大的文件列表( file1file2file3等),对于每个分析,我想引用这个列表中的两个文件(例如function(file1,file2) )。 When I try to do this using paste0("file", pairs[1,x] I get back the character string "file1" rather than the object file1 .当我尝试使用paste0("file", pairs[1,x]执行此操作时,我得到字符串"file1"而不是对象file1

How can I refer to the objects rather than create a character string?如何引用对象而不是创建字符串?

Thank you very much!非常感谢!

Additional comment:附加评论:

  • pairs is a 2xn matrix where each column is the combination of files for one analysis (eg pairs[1,1] = 1 and pairs[2,1] = 2 for the comparison between file1 and file2). pair 是一个 2xn 矩阵,其中每一列是用于一次分析的文件的组合(例如,对于 file1 和 file2 之间的比较,pairs[1,1] = 1 和 pairs[2,1] = 2)。

How to get the variable from a string containing the variable name: 如何从包含变量名称的字符串中获取变量:

> a = 10
> string = "a"
> string
[1] "a"
> eval(parse(text = string))
[1] 10
> eval(parse(text = "a"))
[1] 10

Hope this helps. 希望这可以帮助。

Are you looking for get() ??? 您是否在寻找get()

a <- 1:5
> get("a")
[1] 1 2 3 4 5

另一种选择:eval(as.name("file"))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM