Answers.Body: <p>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):</p>
  <pre><code>v &lt;- lapply(names(x), function(var){
     forest(x[[var]], slab=(my_list[[var]])$StudyID)
  })
  </code></pre>
  <p>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:</p>
  <pre><code>tiff(filename = &quot;~/Desktop/image_%03d.tiff&quot;, width = 30, height = 30, units = &quot;cm&quot;, res = 1200)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-16T13:08:52.173Z
Answers.Text: 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
Answers.UserDisplayName: DEEP
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-12T01:33:03.443Z
Answers.LastActivityDate: 2021-05-12T21:36:04.500Z
Answers.LastEditDate: 2021-05-12T21:36:04.500Z
Answers.UserDisplayName: gringer
Body: <p>I have two lists of dataframes. List <code>x</code> and list <code>my_list</code>. Each having four dataframes and their names are same for the two lists, i.e., <code>bray-curtis, Chao1, FB</code>, and <code>Shannon</code>.</p>
  <p>The dataframes within the two lists have same element names, but they contain different information. This is because list <code>x</code> has been generated after some operations on list <code>my_list</code>.</p>
  <p>Now I want to write a function and run that on list <code>x</code> with `lapply. the command I want to write as a function is</p>
  <pre><code>forest(x<span class="math-container">$Chao1, slab = paste(my_list$</span>Chao1$StudyID))
  </code></pre>
  <p>Here, the first <code>Chao1</code> is coming from list <code>x</code> while the second  <code>Chao1</code> is coming from the list <code>my_list</code>. It works for a single dataframe but I am unable to write the codes with <code>lapply</code> so that it runs across all the dataframes of the lists.</p>
  <p>The desired outputs are four plots from four dataframes of list <code>x</code>.
  Can anyone please help me?</p>
  <p>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:</p>
  <pre><code>tiff(filename = &quot;~/Desktop/image.tiff&quot;, width = 30, height = 30, units = &quot;cm&quot;, res = 1200)
  </code></pre>
  <p>Is there a way to save all them individually?</p>
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-11T23:05:02.517Z
Comments.Text: 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.
Comments.UserDisplayName: Kamil S Jaron
Tags: <r>
Title: How to create this function for lapply?
ThreadText: # How to create this function for lapply?
  
  (Asked by: username_0 on 2021-05-11)
  
  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: @username_2'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?
  
  Thanks
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-05-12. Score: 2 | Accepted answer
  
  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)
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks username_2, 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  
  
  ---
  
  