title: R Loop for Variable Names to run linear regression model
body: <p>First off, I am pretty new to this so my method/thinking may be wrong, I have imported a xlsx data set into a data frame using R and R studio. I want to be able to loop through the column names to get all of the variables with exactly "<em>10</em>" in them in order to run a simple linear regression. So here's my code:</p>
  
  <pre><code>indx &lt;- grepl('_10_', colnames(data)) #list returns all of the true values in the data set
  col10 &lt;- names(data[indx]) #this gives me the names of the columns I want
  </code></pre>
  
  <p>Here is the for loop I have which returns an error:</p>
  
  <pre><code>temp &lt;- c()
  for(i in 1:length(col10)){
     temp = col10[[i]]
    lm.test &lt;- lm(Total_Transactions ~ temp[[i]], data = data)
    print(temp) #actually prints out the right column names
    i + 1
  }
  </code></pre>
  
  <p>Is it even possible to run a loop to place those variables in the linear regression model? The error I am getting is: "Error in model.frame.default(formula = Total_Transactions ~ temp[[i]],  : variable lengths differ (found for 'temp[[i]]')". If anyone could point me in the right direction I would be very grateful. Thanks.</p>