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 to get the names from multiple lists at once?

I have two listing plot_overall_hema and plot_overall_chem. I would like to get get all names from each list to a df with name outputs. How can I achieve this goal?

I can use codes like names(plot_overall_hema) to get the names from each list. Is it a more convenient way to get all names from multiple listing? for example, if I have more than 2 lists, like 10 list?

enter image description here
enter image description here

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

>Solution :

Loop through and get names:

# example lists
plot_overall_chem <- list(one = 1, two = 2)
plot_overall_hema <- list(three = 3, four = 4)


unlist(lapply(ls(pattern = "plot_"), function(i) names(get(i))))
# [1] "one"   "two"   "three" "four

To keep the list names we can stack:

stack(sapply(ls(pattern = "plot_"), function(i) names(get(i)), simplify = FALSE))
#   values               ind
# 1    one plot_overall_chem
# 2    two plot_overall_chem
# 3  three plot_overall_hema
# 4   four plot_overall_hema
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