title: Correspond Indicator Matrix to Column Names in R
body: <p>the question I am trying to solve is "How can I create an automated series of code which will pull the desired column header names from a data set to fit a general linearized model (glm)?" I have a data set with 8 variables; however, I only want to use 3 of them to cross validate and find the "best" model. Here's what I've come up with:</p>
  
  <pre><code>library(boot)
  </code></pre>
  
  <pre><code>salary &lt;- read.csv("salary_data.csv")
  vars &lt;- colnames(salary[c(2,3,7)])
  nvars &lt;- length(vars)
  list.to.expand = vector(mode = "list", length = nvars)
  for (i in 1:nvars){
    list.to.expand[[i]]=c(0,1)
  }
  model.spec.matrix &lt;- expand.grid(list.to.expand)
  </code></pre>
  
  <pre><code>vars
  model.spec.matrix
  </code></pre>
  
  <pre><code>names(model.spec.matrix) &lt;- vars
  vars.to.use &lt;- model.spec.matrix[2,]
  vars.to.use &lt;- as.numeric(vars.to.use)
  cn &lt;- c()
  for (i in 1:nrow(model.spec.matrix)){
    if(i==1){cn &lt;- colnames(model.spec.matrix[sapply(model.spec.matrix[i,], function(x) x &gt; 0)]) 
    }
  }
  print(cn)
  paste(cn, collapse = "+")
  glm.out = glm(paste("LogACG~",paste(cn,collapse = "+"),sep = ""), family = gaussian, data = salary)
  cv.err = cv.glm(salary, glm.out, K = 10)$delta[1]
  </code></pre>
  
  <p>My issue lies in the for loop. I've tried to construct a loop which will append the values from "vars" into "model.use", but I cannot seem to get it to read past the 2nd row in the matrix. Any suggestions? Thanks</p>