title: Using predict() with a vector of specific values in lapply for a list of data.frames
body: <p>I am trying to achieve the following. I have a list of dataframes in the form of:</p>
  
  <pre><code>list1 &lt;- list(d1=data.frame(name=rep("d1",3), A=c(1,2,3), B=c(2,4,5)),
                d2=data.frame(name=rep("d2",3), A=c(1,2,3), B=c(2,4,5)),
                d3=data.frame(name=rep("d3",3), A=c(1,2,3), B=c(2,4,5)))
  </code></pre>
  
  <p>For each dataframe in <code>list1</code>, i want to fit a linear model and then use this model for <code>predict()</code>. The values to use for predictions are in a separate dataframe:</p>
  
  <pre><code>new.values &lt;- data.frame(name=c("d1","d2","d3"), B=c(3,4,5))
  </code></pre>
  
  <p>Each model shall be used with only one value from <code>new.values</code>, the one with the corresponding name (e.g. for <code>list$d1</code> the value in <code>new.values[new.values$name == d1, ]</code>), not for all values in <code>new.values$B</code>
  I tried this:</p>
  
  <pre><code>predictions &lt;- lapply(list1, function(x) predict(lm(A~B, data=x), new.values[new.values$name == names(x),], interval="predict")) 
  </code></pre>
  
  <p>But predictions remains empty: </p>
  
  <pre><code>&gt; predictions
  $d1
       fit lwr upr
  
  $d2
       fit lwr upr
  
  $d3
       fit lwr upr
  </code></pre>
  
  <p>I guess, because R doesnt find any values for predict. If i run</p>
  
  <pre><code>predictions &lt;- lapply(list1, function(x) predict(lm(A~B, data=x), new.values, interval="predict"))  
  </code></pre>
  
  <p>all values in <code>new.values</code> will be used for each model. </p>
  
  <p>How can i fix this?</p>