title: Replace NA by max value in column in a list of data frames
body: <p>I have two data frames and two questions. In both data frames df1 and df2, I can replace the NAs by 0.</p>
  
  <p>df1</p>
  
  <pre><code> var1 &lt;- c(1, NA, 2, NA, 4, 5, 5)
   var2 &lt;- c(1, 2, 3, 4, 5, 6, 7)
   df1 &lt;- data.frame(var1, var2)
   df1$var1[is.na(df1$var1)] &lt;- 0
  </code></pre>
  
  <p>df2</p>
  
  <pre><code> var1 &lt;- c(1, NA, 2, NA, 4, 5, 9)
   var2 &lt;- c(1, 2, 3, 4, 5, 6, 7)
   df2 &lt;- data.frame(var1, var2)
   df2$var1[is.na(df1$var1)] &lt;- 0
  </code></pre>
  
  <p>But how would this work if I wanted to replace the NAs by the maximum value of var1 rather than 0? I thought it would be the following but it does not work.</p>
  
  <pre><code> df1$var1[is.na(df1$var1)] &lt;- max(df1$var1)
  </code></pre>
  
  <p>Once this is solved, I would actually like to do this for a list of data frames using <em>lapply</em>.</p>
  
  <pre><code> mylist &lt;- list(df1, df2)
  </code></pre>
  
  <p>My idea was something like the following - which does not work either.</p>
  
  <pre><code>lapply(mylist, function(x) x$var1[is.na(x$var1)] &lt;- max(x$var1))
  </code></pre>
  
  <p>Many thanks for your help!</p>