[R] turn character string into unevaluated R object

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Mar 3 09:52:18 CET 2010


On Wed, Mar 3, 2010 at 8:26 AM, carol white <wht_crl at yahoo.com> wrote:
> Thanks for all replies but it is still not what I wanted.
>
> Referring to the previous example that I gave, I want that the objects mat1, mat2 and mat3 be created as matrix object. I want to access their value without get() and when I type ls(), I will have
>
> mat1  mat2  mat3
> and not only
> "mat1" "mat2" "mat3"

 When you type 'ls()' it gives you the names of the objects, and the
names of the objects are character strings, so it puts them in quotes.
It will always put them in quotes. You can't have ls() do anything
else.

> and when I type
>
> dim(mat1)
>
> I'll get
> 12 3
> for ex

 You will.

> what I'm looking for is the inverse of deparse(substitution()) which gives the name of the object from the object. I want to access the value of the object directly not through its name.

 How can you access the value of an object not through its name?
That's how you get the value of the object.

> Hope that it's more clear

 Ummm not really. Is this what you want. I have three files called
mat1.txt mat2.txt and mat3.txt. I do:

 for(i in 1:3){
  fileName=paste("mat",i,".txt",sep="")
  obName=paste("mat",i,sep="")
  assign(obName,matrix(scan(fileName,what=1),ncol=3))
 }

 and then I have:

 > ls()
 [1] "fileName" "i"        "mat1"     "mat2"     "mat3"     "obName"
 > dim(mat1)
 [1] 2 3

 Note this is a BAD thing to do, since the next thing you will ask us
is how to get the N'th matrix "matN" for i in 1 to 3. And we will tell
you you should have stuck them in a list first time around.

Is that clear?

Barry
-- 
blog: http://geospaced.blogspot.com/
web: http://www.maths.lancs.ac.uk/~rowlings
web: http://www.rowlingson.com/
twitter: http://twitter.com/geospacedman
pics: http://www.flickr.com/photos/spacedman



More information about the R-help mailing list