title: Regression model function (with user selected variables) on subset of data frame
body: <p>Using data from the fivethirtyeight package...</p>
  
  <pre><code>library(fivethirtyeight)
  grads &lt;- college_recent_grads
  </code></pre>
  
  <p>Created a subset of the grads data to include desired variables</p>
  
  <pre><code>data &lt;- grads[, c("men", "major_category", "employed", 
  "employed_fulltime_yearround", "p25th", 
  "p75th", "total")]
  </code></pre>
  
  <p>Then, I split the <code>data</code> subset up by major category and omitted the one NA value in the data</p>
  
  <pre><code>majorcats &lt;- split(data, data$major_category)
  names(majorcats)
  majorcats &lt;- majorcats %&gt;% na.omit()
  </code></pre>
  
  <p>And tried to run a regression model in a function called facts, where the user could specify x, y, and z, z being a major category (hence why I split up the <code>data</code> subset by major_category)</p>
  
  <pre><code>facts &lt;- function(x, y, z){
     category &lt;- majorcats[["z"]]
     summary(lm(y ~ x, data = category))
   }
  </code></pre>
  
  <p>Unfortunately, when I try to input variables into facts (that are part of the majorcats data set, such as</p>
  
  <pre><code>facts(men, p25th, Arts)
  </code></pre>
  
  <p>I get the error below:</p>
  
  <pre><code>Error in model.frame.default(formula = y ~ x, data = category, 
  drop.unused.levels = TRUE) : 
    invalid type (NULL) for variable 'y'
  Called from: model.frame.default(formula = y ~ x, data = category, 
  drop.unused.levels = TRUE)
  Browse[1]&gt; 
  </code></pre>
  
  <p>Can someone please explain what this error means, and how I might be able to fix it?</p>