I have two lists of dataframes. List x and list my_list. Each having four dataframes and their names are same for the two lists, i.e., bray-curtis, Chao1, FB, and Shannon.

The dataframes within the two lists have same element names, but they contain different information. This is because list x has been generated after some operations on list my_list.

Now I want to write a function and run that on list x with `lapply. the command I want to write as a function is

forest(x$Chao1, slab = paste(my_list$Chao1$StudyID))

Here, the first Chao1 is coming from list x while the second Chao1 is coming from the list my_list. It works for a single dataframe but I am unable to write the codes with lapply so that it runs across all the dataframes of the lists.

The desired outputs are four plots from four dataframes of list x. Can anyone please help me?

Update: @gringer's approach works, but it saves all the plots in a single list. When I tried to save the images with following command it just saved the last image:

tiff(filename = "~/Desktop/image.tiff", width = 30, height = 30, units = "cm", res = 1200)

Is there a way to save all them individually?


  • $\begingroup$ I am still quite confused. You mentioned that the names in my_list and x are the same, why do you need the names from my_list then? Also note you can edit your question and add all these details. Try for example, showing bits of the data.frames in the two lists, or something that would make us understand why you need two lists, not one. $\endgroup$ Commented May 11, 2021 at 23:05

1 Answer 1


Assuming the lists are named, and you want to fetch the element with the same name from each list, I'm guessing something like this would work (or at least get you closer to a solution):

v <- lapply(names(x), function(var){
   forest(x[[var]], slab=(my_list[[var]])$StudyID)

When creating images that don't have a multi-page format (i.e. anything except PDF), and not changing the image name with each save, you need to add in a format specifier to tell R what to do with new created images. For example, this will add a three digit zero-padded number for each new TIFF image:

tiff(filename = "~/Desktop/image_%03d.tiff", width = 30, height = 30, units = "cm", res = 1200)
  • $\begingroup$ Thanks gringer, it really worked. Being new in R, I would like to ask how does this line of code work? I mean, why I have to put double brackets [[]] there? Thanks $\endgroup$
    – DEEP
    Commented May 16, 2021 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.