Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I rename an object using Non standard evaluation in R

Context

I have a named vector q1 = 2. The object name is stored in a = 'q1'.

I can change q1‘s name by names(q1) = 'this_is_name'.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Then I trid to use non standard evaluation to change q1‘s name.

I thought names(q1) = 'new_name' and names(sym(a)) = 'new_name' will do the same, but it is not.

Question

How can I rename the object that get access by a in R?

In other word, How can I make names(sym(a)) = 'new_name' do the same as names(q1) = 'new_name'.

Reproducible code

q1 = 2
names(q1) = 'this_is_name'
a = 'q1'


names(q1) = 'new_name' 

names(sym(a)) = 'new_name' # I expect the code should do the same as names(q1) = 'new_name' 

>Solution :

To actually change the object q1 you can use get and assign, both in base R

assign(a, setNames(get(a), 'new_name'))

q1
#> new_name 
#>        2

Created on 2022-10-09 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading