Sorting Data in R


To sort data in R, we can use the arrange function.

Let’s work with the Palmer penguins dataset. The sort order defaults to ascending order. If we want descending order we just add a minus sign before the column name. Suppose you want to save the data. How do you do that? If we sort it in the console it is not saved. To save it we can create a new name (like penguins2) and then assign it (<-) penguins %>% arrange(bill_length_mm). Now we have the original dataset saved and we can work with our new cleaned dataset called penguins2.

Let’s manually create our own custom data frame and use the arrange function to sort some gemstones by hardness.

library(tidyverse)
# gemstone
gemid <- c(1:6)
gemstones <- c("jade", "ruby", "sapphire", "amethyst", "topaz", "emerald")
gemhard <- c(6.5, 9.0, 7.5, 7.0, 8.0, 7.75)
gemstones <- data.frame(gemid, gemstones,gemhard)
View(gemstones)
gem2 <- gemstones %>% arrange(gemhard)  # the default is ascending
View(gem2)
gem3 <- gemstones %>% arrange(desc(gemhard))
View(gem3)

Leave a Reply