Skip to content

Instantly share code, notes, and snippets.

@chainsawriot
Created July 22, 2014 06:10
Show Gist options
  • Save chainsawriot/ae1fbf91734a72a83f57 to your computer and use it in GitHub Desktop.
Save chainsawriot/ae1fbf91734a72a83f57 to your computer and use it in GitHub Desktop.
iris[c(3,2,1),] # index the dataframe by an "indices vector", will only pick the first three rows in reversed order
order(iris$Sepal.Length) # generate an "indices vector" based on the ranked value of Sepal Length
# therefore
iris[order(iris$Sepal.Length, decreasing=TRUE),]
# is ordered by row based on the value of Sepal Length, you still need to specify the column required.
# try these also
iris[order(iris$Sepal.Length, decreasing=TRUE),1] # just the first col, by index
iris[order(iris$Sepal.Length, decreasing=TRUE),c(1,3)] # 1st and 3rd, by index vector
iris[order(iris$Sepal.Length, decreasing=TRUE),c("Sepal.Length", "Petal.Length")] # by column name vector
iris[order(iris$Species, iris$Sepal.Length, decreasing=TRUE),c("Sepal.Length", "Species")] # order first by Species then by Sepal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment