r - how can I use the object of a variable to represent a variable -


for example,

name <- c("chlyt1","chlyt2")  assign(name[1], 2) assign(name[2], 4)  result <- name[1] 

i want result 2 ,not chlyt1

you could achieve using

result <- get(name[1]) 

but it's unconventional , recipe lot of confusion.


there several approaches deal kind of situations. super simple 1 use simple vector named elements:

values <- c(chlyt1 = 2, chlyt2 = 4) 

then can access values with, obviously, name:

result <- values["chlyt1"] 

or position

result <- values[1] 

or position of name (corresponding approach)

result <- values[names(values)[1]] 

in situations, result equal 2.


Popular posts from this blog