Extract variable names from list or vector in R

Daan11 picture Daan11 · Feb 14, 2017 · Viewed 8.4k times · Source

Assuming:

aa = c('A','B','C')
bb = c('D','E','F')
list1 = list(aa,bb)
vect1 = c(aa,bb)

Is there a way to extract the variable names as strings ('aa', 'bb') from either list1 or vect1? Is this information being stored in lists and vectors? If not, what would be the appropriate format?

Thanks in advance!

Answer

jack jay picture jack jay · Feb 14, 2017

For the situation what you have done the answer is no. But if you are ready to do some changes in your code then you can easily get it,

list1 <- list( aa = aa, bb = bb)

Now you can easily access the string version of names of variables from which list is formed,

names(list1)