Reorder the data frame using arrange
The dplyr
package has a function called arrange
that is used to sort/reorder data frames according to the values of the specified variable. This is often used after we have used the group_by
and summarize
functions as we will see.
Let's suppose we were interested in determining the most frequent destination airports from New York City in 2013:
<- flights %>%
freq_dest group_by(dest) %>%
summarize(num_flights = n())
freq_dest
# A tibble: 105 x 2
dest num_flights
<chr> <int>
1 ABQ 254
2 ACK 265
3 ALB 439
4 ANC 8
5 ATL 17215
6 AUS 2439
7 AVL 275
8 BDL 443
9 BGR 375
10 BHM 297
# ... with 95 more rows
# i Use `print(n = ...)` to see more rows
You'll see that by default the values of dest
are displayed in alphabetical order here. We are interested in finding those airports that appear most:
%>%
freq_dest arrange(num_flights)
# A tibble: 105 x 2
dest num_flights
<chr> <int>
1 LEX 1
2 LGA 1
3 ANC 8
4 SBN 10
5 HDN 15
6 MTJ 15
7 EYW 17
8 PSP 19
9 JAC 25
10 BZN 36
# ... with 95 more rows
# i Use `print(n = ...)` to see more rows
This is actually giving us the opposite of what we are looking for. It tells us the least frequent destination airports first. To switch the ordering to be descending instead of ascending we use the desc
(desc
ending) function:
%>%
freq_dest arrange(desc(num_flights))
# A tibble: 105 x 2
dest num_flights
<chr> <int>
1 ORD 17283
2 ATL 17215
3 LAX 16174
4 BOS 15508
5 MCO 14082
6 CLT 14064
7 SFO 13331
8 FLL 12055
9 MIA 11728
10 DCA 9705
# ... with 95 more rows
# i Use `print(n = ...)` to see more rows