title: Using apply over two lists of different lengths
body: <p>This question is related to my earlier question found here: <a href="https://stackoverflow.com/questions/33089532/r-accounting-for-a-factor-with-this-logistic-regression-function-replace-lappl">https://stackoverflow.com/questions/33089532/r-accounting-for-a-factor-with-this-logistic-regression-function-replace-lappl</a></p>
  
  <p>I realize that I didn't do a good job at asking the first question, so here is a more simple analog with actual data:</p>
  
  <p>My data looks something like this:</p>
  
  <pre><code>#data look like this, but with a variable number of "y" columms
  wk&lt;-rep(1:50,2)
  X&lt;-rnorm(1:100,1)
  y1&lt;-rnorm(1:100,1) 
  y2&lt;-rnorm(1:100,1)
  
  df1&lt;-as.data.frame(cbind(wk,X,y1,y2))
  df1$hyst&lt;-ifelse(df1$wk&gt;=5 &amp; df1$wk&lt;32, "R", "F")
  
  Y&lt;-df1[, -which(colnames(df1) %in% c("wk"))] #this step makes more sense with my actual data since I have a bunch of columns to remove  
  l1&lt;-length(Y)-1
  lst1&lt;-lapply(2:l1,function(x){colnames(Y[x])}) 
  dflst&lt;-c("Y",'Y[Y$hyst=="R",]','Y[Y$hyst=="F",]')
  </code></pre>
  
  <p>I want to run a model over all Y columns for the full data set (all data) and for two subsets, when the factor hyst=="R" and when hyst=="F".<br>
  To do this, I have nested two lapply functions, which sort of works, but I think it essentially doubles my results and is causing me all sorts of list headaches.</p>
  
  <p>Here is the nested lapply code:</p>
  
  <pre><code>lms &lt;- lapply(dflst, function(z){
            lapply(lst1, function(y) {
  form &lt;- paste0(y, " ~ X")
  lm(form, data=eval(parse(text=z)))
   })
  })
  </code></pre>
  
  <p>How can I replace or modify the nested lapply function to obtain a model run for each Y column for each data set( all, "R", and "F")?</p>