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?
>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

