title: Error ... missing value where TRUE/FALSE needed
body: <p>I've been trying to write a code to sort a vector in R, but I keep getting this error message: <code>Error in if (data[j] &lt; data[k]) { : missing value where TRUE/FALSE needed</code></p>
  <p>This is my sort function so far:</p>
  <pre><code>sortscore &lt;- function(data){
     new &lt;- 0
     x &lt;- data
    
     for (i in 1:length(data)){
        new[i] &lt;- minscore(x)
        x &lt;- x[x!=minscore(x)]
        x
     }
     new
  }
  </code></pre>
  <p>This is the minscore function:</p>
  <pre><code>minscore &lt;- function(data){
    j &lt;- 1;k &lt;- j+1; min &lt;- 0
    repeat {
      if(data[j]&lt;data[k]){
        min &lt;- data[j]
        k &lt;- k+1
      }
      else{
        min &lt;- data[k]
        j &lt;- k
        k &lt;- k+1
      }
      if(k==length(data)+1) break
    }
    return(min)
  }
  </code></pre>
  <p>I can only use <code>length()</code> function for a built-in function, hence the need for a sort function. Please help me understand.</p>