title: sapply() Returning Surprising Result - R
body: <p>I am using glm() to create a few different models based on the values in a vector I make (h1_lines).  I want sapply to return a model for each value in the vector.  Instead, my code is currently returning a list of lists where one part of the list is the model.  It seems to be returning everything I do inside the sapply function.</p>
  
  <pre><code>train = data.frame(scores=train[,y_col], total=train[,4], history=train[,5], line=train[,6])
  h1_lines&lt;- c(65, 70, 75)
  
  models &lt;- sapply(h1_lines, function(x){
                   temp_set&lt;-train
                   temp_set$scores&lt;-ifelse(temp_set$scores&gt;x,1,
                                       ifelse(temp_set$scores&lt;x,0,rbinom(dim(temp_set)[1],1,.5)))
  
                   mod&lt;-glm(scores ~ total + history + line, data=temp_set, family=binomial)
                                      })
  </code></pre>
  
  <p>I'd like the code to work so after these lines I can do:</p>
  
  <pre><code>predict(models[1,], test_case)
  predict(models[2,], test_case)
  predict(models[3,], test_case)
  </code></pre>
  
  <p>But right now I can't do it cause sapply is returning more than just the model... If I do print(dim(models)) it says models has 30 rows and 3 columns??</p>
  
  <p>EDIT TO ADD QUESTION; </p>
  
  <p>Using the suggestion below code works great, I can do predict(models[[1]], test_case) and it works perfectly.  How can I return/save the models so I can access them with the key I used to create them? For example, using the h1_scores it could be something like the following:</p>
  
  <pre><code>predict(models[[65]], test_case))
  
  predict(models[[key==65]], test_case)
  </code></pre>