0

Everything is said in the title. I'm looking for the R equivalent of Python's heapq.heapify(). I could not find it, but I hope it exists somewhere.

heapq is a heap queue implementation module and its member function heapify accepts as input a list of objects and returns a heap object containing references to those objects as members of the heap.

EDIT: I cannot use rPython to call Python code within R because the final script will be part of a Shiny app (which barely works with rPython, and does not allow importing modules anyway).

Antoine
  • 1,649
  • 4
  • 23
  • 50
  • Quick googling turns up http://www.inside-r.org/packages/cran/Containers but I'm not qualified to comment on its usefulness. – tripleee Dec 14 '15 at 09:53
  • @tripleee thanks. The package Containers was apparently removed from CRAN but I'll try to download the archive and see if the source code for heap functions is written in R so I can source it within my Shiny app – Antoine Dec 14 '15 at 10:06
  • unless someone from `r` comes around and says that it is available from the standard packages, then this is a request for a resource and/or tool, and that is off-topic – Drew Dec 14 '15 at 10:12
  • @Drew how is my question different from the dozens of other questions [here](http://stackoverflow.com/search?q=R+equivalent+of+Python)? – Antoine Dec 14 '15 at 10:34
  • Many of the questions in your result ask about a core concept, such as how to split a string on a delimiter or what data type to use when you know the corresponding Python concept. The remaining quests for libraries should arguably be closed as well. See http://meta.stackoverflow.com/questions/251134/where-can-i-ask-about-finding-a-tool-library-or-favorite-off-site-resource ... I also tried to search for a "why was mine closed when these other guys were not even tho they are bad too" but couldn't quickly find one; but the answer should be trivially obvious. – tripleee Dec 14 '15 at 10:45
  • ... Maybe this one: http://meta.stackexchange.com/questions/138252/why-was-this-question-asking-for-a-library-recommendation-closed-as-not-construc – tripleee Dec 14 '15 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97839/discussion-between-tripleee-and-antoine). – tripleee Dec 14 '15 at 11:03

1 Answers1

1

I created this heapify R function by porting the Matlab code found here (that was easier than porting from Python because like R, Matlab uses 1-indexes baseline).

Just pass an unsorted vector to the function, and specify whether you want min-heapify or max-heapify to be implemented. I compared with Python's heapq.heapify() (which is min-heapify by default) and the results are the same.

heapify = function(input_vector, min){

  # below is the maxheapify algorithm
  # minheapify = maxheapify with sign change at the beginning and at the end

  if (min==TRUE){input_vector = - input_vector}

    count = length(input_vector)

    start = floor(count/2)

    while (start >= 1){
      root = start
      theEnd = count

      while ((root * 2) <= theEnd){

        child = root * 2

        if ((child + 1 <= theEnd) & (input_vector[child] < input_vector[child+1])){
          child = child + 1
        }

        if (input_vector[root] < input_vector[child]){

          temp = input_vector[root]
          input_vector[root] = input_vector[child]
          input_vector[child] = temp

          root = child

        } else {
          break
        }

      }

        start = start - 1
    }

    if (min==TRUE){input_vector = - input_vector}

output = list(heap=input_vector)
}

Example:

in R:

heapify(c(30,1,1,0,3,3,3,2,14,25,3,10,4,3),min=TRUE)$heap
[1]  0  1  1  2  3  3  3 30 14 25  3 10  4  3

in Python:

test = [30,1,1,0,3,3,3,2,14,25,3,10,4,3]
heapq.heapify(test)
test
Out: [0, 1, 1, 2, 3, 3, 3, 30, 14, 25, 3, 10, 4, 3]
Antoine
  • 1,649
  • 4
  • 23
  • 50