title: List of dataframes to one DF; Extract date column from each DF in list, pass all values to single DF w/ columns 1985-2017)
body: <p>I have a list of 169 dataframes (<em>assetcount_dfs</em>) corresponding to squares on a geographical grid that each contain a bundle of assets. I would like to fill a separate dataframe counting the number of assets that begin on each date, per square, for years 1985-2017.</p>
  
  <p>Here's how this list of dataframes is structured:</p>
  
  <pre><code> Square1_DF (3 rows/assets)   | x | y | dates char[1989, N/A, 1991]
   ...
   Square169_DF (1 row/asset)   | x | y | dates char[2002]
  </code></pre>
  
  <p>I want to convert this to one dataframe counting these dates, in '<em>dateDF</em>':  </p>
  
  <pre><code>            | 1989 | 1990 | ... | 2015 | 2016 | 2017 
   Square 1      0      1            3      2      0      
   ...
   Square 169    0      0            0      1      3
  </code></pre>
  
  <p>Here's a toy sample of my data. Within each of the data frames in <code>assetcount_dfs</code>, the 'val' column represents the dates I want to populate <em>dateDF</em> with:</p>
  
  <pre><code>  sdf1 &lt;- data.frame(a = c("1","4","5","1"), x = c("sdf","asf","asdf","sdf"), val = c("2014","2012","#N/A", "2001"))
    sdf2 &lt;- data.frame(a = c("1","4"), x = c("sdf","asdf"), val = c("#N/A","2011"))
    sdf3 &lt;- data.frame(a = c("1","4","5","1","1"), x = c("sdf","asf","asdf","sdf","sdf"), val = c("2010","2015","2000","2002", "2003"))
  
    assetcount_dfs &lt;- list(sdf1 = sdf1,sdf2 = sdf2,sdf3 = sdf3)
  
    date_range &lt;- 1985:2017
    dateDF &lt;- data.frame(matrix(ncol = length(date_range),nrow = 3))     # actual length is 169 rows, only using 3 for this example
    colnames(dateDF) &lt;- paste0('X',1985:2017) # name columns 'X'DATE
    rownames(dateDF) &lt;- names(assetcount_dfs)
    dateDF[] &lt;- 0          # filled with zeroes     
  </code></pre>
  
  <hr>
  
  <h1>Current attempt</h1>
  
  <p>Within each dataframe's 'val' column, I want to check if any of the date values were in the range 1985-2017, and if so, add them to dateDF's X--- date column.</p>
  
  <p>I tried using 'purr' (like lapply) to operate on each DF but I'm struggling to understand where to go from here.</p>
  
  <pre><code>invisible(map(listx, function(df) {
  
  for (i in df$val){
      if (as.integer(i) %in% 1985:2017){
      datesDF_colName &lt;- paste0('X',i)
      dateDF[substitute(df), datesDF_colName] &lt;- dateDF[[datesDF_colName]] + 1 
        # Attempt to set dateDF value at [grid-square DF's name / row, Column based on Year ]
      } 
  
  }}))
  
  # Output:    
  # Error in `[&lt;-.data.frame`(`*tmp*`, substitute(df), datesDF_colName, value = 
  # c(1,  : 
  #  anyNA() applied to non-(list or vector) of type 'language'
  # Called from: `[&lt;-.data.frame`(`*tmp*`, substitute(df), datesDF_colName, 
  # value = c(1, 
  # 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  # 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  
  # Note my sample code for 'listx' for some reason generates DFs with factors, although I am currently dealing with character arrays.
  </code></pre>