R : Finding the ordering index vector

The ordering index vector is a vector that contains indices of another vector.  It’s basically the other vector, but ordered (decreasing or increasing).  Instead of having the values it has the indices :)  I spent close to an hour to find this line :) And here it is.  In all its glory :)

sort.int(VECTOR, index.return=TRUE)

The result of this command is a bit strange (for me).  It returns an object that contains the ordered vector and the ordered index vector in a list.  Long story short, I can’t access them like I would want to.  So the command I ended up using is a bit more complex:

unlist(as.matrix(sort.int(VECTOR, index.return=TRUE))[2])

Notice that I coerce the resulting list into a matrix.  This results in a matrix containing two elements: [1]->ordered vector and [2]->  ordered index vector. Therefore I chose the second element in the returned list and unlisted it into a vector.  This is what I wanted as I can index a vector :)

Advertisement

About joelgranados

I'm fascinated with how technology and science impact our reality and am drawn to leverage them in order to increase the potential of human activity.
This entry was posted in commands, R. Bookmark the permalink.

3 Responses to R : Finding the ordering index vector

  1. Kulvait says:

    sort.int(VECTOR, index.return=TRUE)$ix is the way where to go.

  2. W.F says:

    Wouldn’t using order(VECTOR) do the trick? it returns a vector and is easy to use :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s