[R] accessing an object with a variable ?

Marius 't Hart mariusthart at gmail.com
Sat Jan 18 22:55:09 CET 2014


Does this help you?

http://stackoverflow.com/questions/3971844/access-variable-value-where-the-name-of-variable-is-stored-in-a-string-in-r

On 14-01-18 04:21 PM, ce 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