Sort dataframe by column value (r)

Viewed 218

I am new to R and currently trying to wrap my head around dataframes in R. I want to sort a dataframe by the column values, and then return the top of it after it has been sorted.

As of now I only seem to get one row back. I used the "iris" dataframe.

sort <- function(df, var.name, n){
  df1 <- df[rev(order(var.name)), ]
  sorted <- head(df1, n)
  return(sorted)
}

sort_head(df = iris, var.name = "Petal.Length", n = 10)
# My output
> sort_head(df = iris, var.name = "Petal.Length", n = 5)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa

My ordering of the dataframe seems to rewrite the dataframe to only contain one row - whereas all the guides (e.g here) I've found simply rewrites the dataframe to be sorted by column. What am I missing?

2 Answers

Try arrange() and slice(), part of {dplyr} and the {tidyverse}:

library(tidyverse)
iris %>%
  arrange(-Petal.Length) %>% # arrange by var in descending (-) order
  slice(1:10) # slice out rows 1 through 10

Returns:

   Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1           7.7         2.6          6.9         2.3 virginica
2           7.7         3.8          6.7         2.2 virginica
3           7.7         2.8          6.7         2.0 virginica
4           7.6         3.0          6.6         2.1 virginica
5           7.9         3.8          6.4         2.0 virginica
6           7.3         2.9          6.3         1.8 virginica
7           7.2         3.6          6.1         2.5 virginica
8           7.4         2.8          6.1         1.9 virginica
9           7.7         3.0          6.1         2.3 virginica
10          6.3         3.3          6.0         2.5 virginica

If you liked this approach, I highly recommend walking through the examples of Chapter 5 of r4ds which covers {dplyr}. This is bound to save you countless hours in the future when scratching your head about data.frame transformations. =)

While df[rev(order(df$Sepal.Length)),] will work outside the function ->

Inside the function we have to switch from $ to [[ and pass the function arguments as strings:

Therefore the correct term inside the function would be: df[rev(order(df[[var.name]])), ]

sort_head <- function(df, var.name, n){
    df1 <- df[rev(order(df[[var.name]])), ]
    sorted <- head(df1, n)
    return(sorted)
}

sort_head(df = iris, var.name = "Petal.Length", n = 10)

Output:

sort_head(df = iris, var.name = "Petal.Length", n = 10)
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
119          7.7         2.6          6.9         2.3 virginica
123          7.7         2.8          6.7         2.0 virginica
118          7.7         3.8          6.7         2.2 virginica
106          7.6         3.0          6.6         2.1 virginica
132          7.9         3.8          6.4         2.0 virginica
108          7.3         2.9          6.3         1.8 virginica
136          7.7         3.0          6.1         2.3 virginica
131          7.4         2.8          6.1         1.9 virginica
110          7.2         3.6          6.1         2.5 virginica
126          7.2         3.2          6.0         1.8 virginica
Related