title: Validating arguments before executing a function
body: <p>I want to validate that each of the two arguments I'm passing to a function are valid. </p>
  
  <p>To do this, I'm checking to see if the 'State' value I assign to my 'state' variable is in a list of states, 'statelist'. Then I check to see if the 'Measure' value I assign to my 'measure' variable is in my list of measures, 'measurelist'. If the first check fails, an "invalid state" error message should be generated. If the second check fails, an "invalid measure" error message should be produced. If both checks pass, I want the function lowest &lt;- function(state, measure) to run.</p>
  
  <p>This is my dataframe, 'data':</p>
  
  <pre><code>school&lt;-c("NYU", "BYU", "USC", "FIT", "Oswego","UCLA","USF","Columbia")
  state&lt;-c("NY","UT","CA","NY","NY","CA", "CA","NY")
  measure&lt;-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
  score&lt;-c(590, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
  data&lt;-data.frame(school,state, measure,score)
  </code></pre>
  
  <p>This function checks that I have a valid state abbreviation:</p>
  
  <pre><code>teststate&lt;-function(state){
    statelist&lt;-(c(state.abb,"DC","GU","MP","PR","VI"))
    if(state %in% statelist){
      testmeasure(measure)
    } else{
      stop("invalid state")
    }
   } 
  </code></pre>
  
  <p>This function checks that the measure I'm specifying is in my list of measures: </p>
  
  <pre><code>testmeasure&lt;-function(measure){ 
      measurelist&lt;-c("MSAT","GPA","VSAT")
      if(measure %in% measurelist){
        lowest(state, measure)
      } else{
        stop("invalid measure")
      }
    }
  </code></pre>
  
  <p>This is the main function that should run after the validity checks are successful:</p>
  
  <pre><code>lowest &lt;- function(state, measure){
      answer&lt;-subset(data,subset=(state==State &amp; measure==Measure))
      order.answer&lt;-order(answer$score,answer$school)
      answer1&lt;-as.matrix(answer[order.answer,])
      answer1[1,1]
    }
  </code></pre>
  
  <p>For this example:</p>
  
  <pre><code>lowest("NY","MSAT")
  </code></pre>
  
  <p>the correct answer should be [1] "Owsego", but when I run the code in its entirety, it never completes, and the console simply shows:</p>
  
  <pre><code>+ lowest("NY","MSAT")
  + 
  </code></pre>
  
  <p>When I tested each of these modules independently, outside of a function, they worked.</p>