[R] accessing an object with a variable ?

arun smartpink111 at yahoo.com
Sat Jan 18 23:03:49 CET 2014


May be this helps:
 assign("a",eval(parse(text=b)))
A.K.




On Saturday, January 18, 2014 4:24 PM, ce <zadig_1 at excite.com> wrote:

I want access and object , its name is in an variable :

require(xts)
data(sample_matrix)

head(sample_matrix)
               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
> assign("a",sample_matrix)
> head(a)

               Open     High      Low    Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185


But following doesn't work:

b="sample_matrix"
assign("a",b)

> a
[1] "sample_matrix"

how I can access sample_matrix data when I have only b variable ?

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list