title: Map within Map? Working with columns within two lists of dataframes
body: <p>I'm working in R and have two lists of dataframes. I need to add a column to the dataframes within list1, filled with values taken from corresponding dataframes within list2. I think I need Map within Map, or lapply within Map, but I have only ever used Map and lapply on their own, or a lapply within a lapply so I am having trouble getting the correct code sorted. The below lists are similar to what I am working with.</p>
  
  <pre><code>dfa&lt;-data.frame(tom=sample(seq(as.Date("2020-01-01"),as.Date("2020-03-01"), 1), 20), id=seq(1:20))
  dfb&lt;-data.frame(tom=sample(seq(as.Date("2020-01-01"),as.Date("2020-02-01"), 1), 32), id=seq(1:32))
  list1&lt;-list(dfa, dfb)
  
  df1&lt;-data.frame(date=seq(as.Date("2020-01-01"), as.Date("2020-03-01"), 1),num=sample(1:200, 61))
  df2&lt;-data.frame(date=seq(as.Date("2020-01-01"), as.Date("2020-03-01"), 1),num=sample(4:200, 61))
  list2&lt;-list(df1, df2)
  </code></pre>
  
  <p>The code that I have tried might be close or totally off mark. I am trying to add the new column "d1" to all dataframes in list1. The values in d1 should be pulled from variable "num" in the dataframes of list2, where the dates match to the dates from dataframes in list1. </p>
  
  <pre><code>list3&lt;-Map(function(x, y){
             Map(function(v, w){
                 v&lt;-cbind(v, d1=w[w$date==v, w$num])}, x$tom, y)},list1, list2)
  </code></pre>
  
  <p>This just returns an error regarding use of atomic vectors: "Error in w$date : $ operator is invalid for atomic vectors"</p>
  
  <p>But if I change it to indexing...</p>
  
  <pre><code>list3&lt;-Map(function(x, y){
         Map(function(v, w){
             v&lt;-cbind(v, d1=w[w[,1]==v, w[,2]])}, x$tom, y)}, list1, list2)
  </code></pre>
  
  <p>It is not happy either: "Error in <code>[.default</code>(w, , 1) : incorrect number of dimensions"</p>
  
  <p>By this point I've sort of lost track of what I'm trying to refer to in the innermost function and how to refer to it properly. Is this even getting close to what I want?</p>