Error, missing value where TRUE/FALSE needed

I am using this within a heapsort function, fifth line of the maxheapify function (if statement) is returning an error and I don't know how to fix it.

maxheapify = function(x,i,heapsize){
   ll = 2*i
   rr = ll + 1
   largest = i 
   if ((ll <= heapsize) && (x[ll] > x[i])){
     largest = ll
   }
   if (largest != i){
     x = swap(x,i,largest)
     x = maxheapify(x,largest,heapsize)
   }
 }
 
 swap = function(x,i,j){
   val = x[i]
   x[i] = x[j]
   x[j] = val
 }

It's not obvious to me what you are trying to do, but I throw a few observations out there.

I'm not sure what inputs you used, so I used maxheapify(1, 2, 4)

In my example run of your code i is a scaler value of 2 and x is 1. so the subsetting (both x[ll] and x[i]) return NA. So the code throws an error.

What inputs were you using? What output were you expecting?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.