library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(nycflights13)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ ggplot2 3.4.3 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
data(flights)
flights |>
filter(arr_delay >= 120)
## # A tibble: 10,200 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 811 630 101 1047 830
## 2 2013 1 1 848 1835 853 1001 1950
## 3 2013 1 1 957 733 144 1056 853
## 4 2013 1 1 1114 900 134 1447 1222
## 5 2013 1 1 1505 1310 115 1638 1431
## 6 2013 1 1 1525 1340 105 1831 1626
## 7 2013 1 1 1549 1445 64 1912 1656
## 8 2013 1 1 1558 1359 119 1718 1515
## 9 2013 1 1 1732 1630 62 2028 1825
## 10 2013 1 1 1803 1620 103 2008 1750
## # ℹ 10,190 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
IAH or HOU)flights |>
filter(dest %in% c("IAH", "HOU"))
## # A tibble: 9,313 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 623 627 -4 933 932
## 4 2013 1 1 728 732 -4 1041 1038
## 5 2013 1 1 739 739 0 1104 1038
## 6 2013 1 1 908 908 0 1228 1219
## 7 2013 1 1 1028 1026 2 1350 1339
## 8 2013 1 1 1044 1045 -1 1352 1351
## 9 2013 1 1 1114 900 134 1447 1222
## 10 2013 1 1 1205 1200 5 1503 1505
## # ℹ 9,303 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights |>
filter(carrier %in% c("UA", "AA", "DL"))
## # A tibble: 139,504 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 554 600 -6 812 837
## 5 2013 1 1 554 558 -4 740 728
## 6 2013 1 1 558 600 -2 753 745
## 7 2013 1 1 558 600 -2 924 917
## 8 2013 1 1 558 600 -2 923 937
## 9 2013 1 1 559 600 -1 941 910
## 10 2013 1 1 559 600 -1 854 902
## # ℹ 139,494 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights |>
filter(month %in% c(7,8,9))
## # A tibble: 86,326 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 7 1 1 2029 212 236 2359
## 2 2013 7 1 2 2359 3 344 344
## 3 2013 7 1 29 2245 104 151 1
## 4 2013 7 1 43 2130 193 322 14
## 5 2013 7 1 44 2150 174 300 100
## 6 2013 7 1 46 2051 235 304 2358
## 7 2013 7 1 48 2001 287 308 2305
## 8 2013 7 1 58 2155 183 335 43
## 9 2013 7 1 100 2146 194 327 30
## 10 2013 7 1 100 2245 135 337 135
## # ℹ 86,316 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights |>
filter(arr_delay > 120) |>
filter(dep_delay <= 0)
## # A tibble: 29 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 27 1419 1420 -1 1754 1550
## 2 2013 10 7 1350 1350 0 1736 1526
## 3 2013 10 7 1357 1359 -2 1858 1654
## 4 2013 10 16 657 700 -3 1258 1056
## 5 2013 11 1 658 700 -2 1329 1015
## 6 2013 3 18 1844 1847 -3 39 2219
## 7 2013 4 17 1635 1640 -5 2049 1845
## 8 2013 4 18 558 600 -2 1149 850
## 9 2013 4 18 655 700 -5 1213 950
## 10 2013 5 22 1827 1830 -3 2217 2010
## # ℹ 19 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights |>
filter(dep_delay >= 60) |>
filter(air_time > 30)
## # A tibble: 26,657 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 811 630 101 1047 830
## 2 2013 1 1 826 715 71 1136 1045
## 3 2013 1 1 848 1835 853 1001 1950
## 4 2013 1 1 957 733 144 1056 853
## 5 2013 1 1 1114 900 134 1447 1222
## 6 2013 1 1 1120 944 96 1331 1213
## 7 2013 1 1 1301 1150 71 1518 1345
## 8 2013 1 1 1337 1220 77 1649 1531
## 9 2013 1 1 1400 1250 70 1645 1502
## 10 2013 1 1 1505 1310 115 1638 1431
## # ℹ 26,647 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights to find the flights with longest
departure delays. Find the flights that left earliest in the
morning.twoflights <- flights
twoflights$dep_time[twoflights$dep_time == 2400]<- 0
#Longest delays
twoflights |>
arrange(desc(dep_delay))
## # A tibble: 336,776 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <dbl> <int> <dbl> <int> <int>
## 1 2013 1 9 641 900 1301 1242 1530
## 2 2013 6 15 1432 1935 1137 1607 2120
## 3 2013 1 10 1121 1635 1126 1239 1810
## 4 2013 9 20 1139 1845 1014 1457 2210
## 5 2013 7 22 845 1600 1005 1044 1815
## 6 2013 4 10 1100 1900 960 1342 2211
## 7 2013 3 17 2321 810 911 135 1020
## 8 2013 6 27 959 1900 899 1236 2226
## 9 2013 7 22 2257 759 898 121 1026
## 10 2013 12 5 756 1700 896 1058 2020
## # ℹ 336,766 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
#Earliest flights
twoflights |>
arrange(dep_time)
## # A tibble: 336,776 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <dbl> <int> <dbl> <int> <int>
## 1 2013 10 30 0 2359 1 327 337
## 2 2013 11 27 0 2359 1 515 445
## 3 2013 12 5 0 2359 1 427 440
## 4 2013 12 9 0 2359 1 432 440
## 5 2013 12 9 0 2250 70 59 2356
## 6 2013 12 13 0 2359 1 432 440
## 7 2013 12 19 0 2359 1 434 440
## 8 2013 12 29 0 1700 420 302 2025
## 9 2013 2 7 0 2359 1 432 436
## 10 2013 2 7 0 2359 1 443 444
## # ℹ 336,766 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
flights to find the fastest flights. (Hint: Try
including a math calculation inside of your function.)flights |>
mutate(speed = distance/air_time) |>
select(speed, flight) |>
arrange(desc(speed))
## # A tibble: 336,776 × 2
## speed flight
## <dbl> <int>
## 1 11.7 1499
## 2 10.8 4667
## 3 10.8 4292
## 4 10.7 3805
## 5 9.86 1902
## 6 9.4 315
## 7 9.29 707
## 8 9.27 936
## 9 9.24 347
## 10 9.24 1503
## # ℹ 336,766 more rows
flights |>
select(month, day) |>
distinct()
## # A tibble: 365 × 2
## month day
## <int> <int>
## 1 1 1
## 2 1 2
## 3 1 3
## 4 1 4
## 5 1 5
## 6 1 6
## 7 1 7
## 8 1 8
## 9 1 9
## 10 1 10
## # ℹ 355 more rows
Yes there was a flight for every day in 2013. After only selecting by month and day, then getting rid of duplicates, only 365 rows remained which is a whole year.
flights |>
select(distance, flight) |>
arrange(desc(distance))
## # A tibble: 336,776 × 2
## distance flight
## <dbl> <int>
## 1 4983 51
## 2 4983 51
## 3 4983 51
## 4 4983 51
## 5 4983 51
## 6 4983 51
## 7 4983 51
## 8 4983 51
## 9 4983 51
## 10 4983 51
## # ℹ 336,766 more rows
flights |>
select(distance, flight) |>
arrange((distance))
## # A tibble: 336,776 × 2
## distance flight
## <dbl> <int>
## 1 17 1632
## 2 80 3833
## 3 80 4193
## 4 80 4502
## 5 80 4645
## 6 80 4193
## 7 80 4619
## 8 80 4619
## 9 80 4619
## 10 80 4619
## # ℹ 336,766 more rows
Flight 51 traveled the farthest distance of 4983 miles. Flight 1632 traveled the shortest distance of 17 miles.
filter() and
arrange() if you’re using both? Why/why not? Think about
the results and how much work the functions would have to do.No. Filter brings out specific column values while arrange changes the order of the rows. However, it does not matter which is used first because it will yield the same result
dep_time, dep_delay, arr_time,
and arr_delay from flights.flights |>
select(dep_time, dep_delay, arr_time, arr_delay)
## # A tibble: 336,776 × 4
## dep_time dep_delay arr_time arr_delay
## <int> <dbl> <int> <dbl>
## 1 517 2 830 11
## 2 533 4 850 20
## 3 542 2 923 33
## 4 544 -1 1004 -18
## 5 554 -6 812 -25
## 6 554 -4 740 12
## 7 555 -5 913 19
## 8 557 -3 709 -14
## 9 557 -3 838 -8
## 10 558 -2 753 8
## # ℹ 336,766 more rows
flights |>
select("dep_time", "dep_delay", "arr_time", "arr_delay")
## # A tibble: 336,776 × 4
## dep_time dep_delay arr_time arr_delay
## <int> <dbl> <int> <dbl>
## 1 517 2 830 11
## 2 533 4 850 20
## 3 542 2 923 33
## 4 544 -1 1004 -18
## 5 554 -6 812 -25
## 6 554 -4 740 12
## 7 555 -5 913 19
## 8 557 -3 709 -14
## 9 557 -3 838 -8
## 10 558 -2 753 8
## # ℹ 336,766 more rows
flights |>
select(4, 6, 7, 9)
## # A tibble: 336,776 × 4
## dep_time dep_delay arr_time arr_delay
## <int> <dbl> <int> <dbl>
## 1 517 2 830 11
## 2 533 4 850 20
## 3 542 2 923 33
## 4 544 -1 1004 -18
## 5 554 -6 812 -25
## 6 554 -4 740 12
## 7 555 -5 913 19
## 8 557 -3 709 -14
## 9 557 -3 838 -8
## 10 558 -2 753 8
## # ℹ 336,766 more rows
select() call?flights |>
select(month, month, day, year)
## # A tibble: 336,776 × 3
## month day year
## <int> <int> <int>
## 1 1 1 2013
## 2 1 1 2013
## 3 1 1 2013
## 4 1 1 2013
## 5 1 1 2013
## 6 1 1 2013
## 7 1 1 2013
## 8 1 1 2013
## 9 1 1 2013
## 10 1 1 2013
## # ℹ 336,766 more rows
If you specify the same variable multiple times in the select function, it is only selected once.
any_of() function do? Why might it be
helpful in conjunction with this vectorflights |> select(any_of(variables <- c("year", "month", "day", "dep_delay", "arr_delay")))
## # A tibble: 336,776 × 5
## year month day dep_delay arr_delay
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 1 2 11
## 2 2013 1 1 4 20
## 3 2013 1 1 2 33
## 4 2013 1 1 -1 -18
## 5 2013 1 1 -6 -25
## 6 2013 1 1 -4 12
## 7 2013 1 1 -5 19
## 8 2013 1 1 -3 -14
## 9 2013 1 1 -3 -8
## 10 2013 1 1 -2 8
## # ℹ 336,766 more rows
Any_of is a selection helped that selects variables contained in a character vector. Any of doesn’t for missing or unknown variables. Helpful when you want to make sure a variable is removed. It would be helpful because it can ignore the quotations that may hold the function back.
flights |> select(contains("TIME"))
## # A tibble: 336,776 × 6
## dep_time sched_dep_time arr_time sched_arr_time air_time time_hour
## <int> <int> <int> <int> <dbl> <dttm>
## 1 517 515 830 819 227 2013-01-01 05:00:00
## 2 533 529 850 830 227 2013-01-01 05:00:00
## 3 542 540 923 850 160 2013-01-01 05:00:00
## 4 544 545 1004 1022 183 2013-01-01 05:00:00
## 5 554 600 812 837 116 2013-01-01 06:00:00
## 6 554 558 740 728 150 2013-01-01 05:00:00
## 7 555 600 913 854 158 2013-01-01 06:00:00
## 8 557 600 709 723 53 2013-01-01 06:00:00
## 9 557 600 838 846 140 2013-01-01 06:00:00
## 10 558 600 753 745 138 2013-01-01 06:00:00
## # ℹ 336,766 more rows
Yes this result is surprising because code in usually case-sensitive. Select helpers deal with upper and lower case by default by ignoring case. However if someone wanted to change that they could use ignore.case = FALSE.
flights |>
rename(air_time_min = air_time) |>
relocate(air_time_min)
## # A tibble: 336,776 × 19
## air_time_min year month day dep_time sched_dep_time dep_delay arr_time
## <dbl> <int> <int> <int> <int> <int> <dbl> <int>
## 1 227 2013 1 1 517 515 2 830
## 2 227 2013 1 1 533 529 4 850
## 3 160 2013 1 1 542 540 2 923
## 4 183 2013 1 1 544 545 -1 1004
## 5 116 2013 1 1 554 600 -6 812
## 6 150 2013 1 1 554 558 -4 740
## 7 158 2013 1 1 555 600 -5 913
## 8 53 2013 1 1 557 600 -3 709
## 9 140 2013 1 1 557 600 -3 838
## 10 138 2013 1 1 558 600 -2 753
## # ℹ 336,766 more rows
## # ℹ 11 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
## # flight <int>, tailnum <chr>, origin <chr>, dest <chr>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
#flights |>
#select(tailnum) |>
#arrange(arr_delay)
It does not work because by selecting tailnum, all other columns go away so arranging by arrival delay is impossible because it’s not there.
flights |> group_by(carrier, dest) |> summarize(n()))flights |>
group_by(carrier) |>
summarize(avg_delay = mean(arr_delay, na.rm = TRUE)) |>
arrange(desc(avg_delay))
## # A tibble: 16 × 2
## carrier avg_delay
## <chr> <dbl>
## 1 F9 21.9
## 2 FL 20.1
## 3 EV 15.8
## 4 YV 15.6
## 5 OO 11.9
## 6 MQ 10.8
## 7 WN 9.65
## 8 B6 9.46
## 9 9E 7.38
## 10 UA 3.56
## 11 US 2.13
## 12 VX 1.76
## 13 DL 1.64
## 14 AA 0.364
## 15 HA -6.92
## 16 AS -9.93
The F9 carrier has the worst average delays.
flights |>
group_by(dest) |>
slice_max(dep_delay, n=1) |>
select(dep_delay, dest)
## # A tibble: 105 × 2
## # Groups: dest [105]
## dep_delay dest
## <dbl> <chr>
## 1 142 ABQ
## 2 219 ACK
## 3 323 ALB
## 4 75 ANC
## 5 898 ATL
## 6 351 AUS
## 7 222 AVL
## 8 252 BDL
## 9 248 BGR
## 10 325 BHM
## # ℹ 95 more rows
flights |>
ggplot(aes(sched_dep_time, dep_time)) +
geom_point()
## Warning: Removed 8255 rows containing missing values (`geom_point()`).
As it gets later, flights start to get more delayed.
n to
slice_min() and friends?flights |>
group_by(dest) |>
slice_max(dep_delay, n=-1) |>
select(dep_delay, dest)
## # A tibble: 336,762 × 2
## # Groups: dest [103]
## dep_delay dest
## <dbl> <chr>
## 1 142 ABQ
## 2 139 ABQ
## 3 125 ABQ
## 4 125 ABQ
## 5 119 ABQ
## 6 117 ABQ
## 7 112 ABQ
## 8 107 ABQ
## 9 106 ABQ
## 10 105 ABQ
## # ℹ 336,752 more rows
It shows all the rows instead of just the minimum value.
count() does in terms of the dplyr
verbs you just learned. What does the sort argument to
count() do?Count() lets you count the unique values of one or more variables. It is equivalent to grouping and then summarizing. The sort argument shows the largest groups at the top of the count. This is equivalent to arranging.
df <- tibble(
x = 1:5,
y = c("a", "b", "a", "a", "b"),
z = c("K", "K", "L", "L", "K")
)
df
## # A tibble: 5 × 3
## x y z
## <int> <chr> <chr>
## 1 1 a K
## 2 2 b K
## 3 3 a L
## 4 4 a L
## 5 5 b K
I think that the output will look exactly like the regular data frame.
df |>
group_by(y)
## # A tibble: 5 × 3
## # Groups: y [2]
## x y z
## <int> <chr> <chr>
## 1 1 a K
## 2 2 b K
## 3 3 a L
## 4 4 a L
## 5 5 b K
I was correct. Group_by groups the rows so that any action that occurs next will happen in those subsequent groups. However, since there was no action afterwards, the output stayed the same
The output will have the y values all grouped the same starting with ‘a’ and going down alphabetically.
df |>
arrange(y)
## # A tibble: 5 × 3
## x y z
## <int> <chr> <chr>
## 1 1 a K
## 2 3 a L
## 3 4 a L
## 4 2 b K
## 5 5 b K
I was right. Arrange puts all the selected column values that are the same or similar together and in this case it going from a-z. This s different from group_by becuase group_by just focuses on the next actions and groups them in that way.
I think that this will make three columns of y, x, and mean_x. Mean_x will should the mean x value of every y value.
df |>
group_by(y) |>
summarize(mean_x = mean(x))
## # A tibble: 2 × 2
## y mean_x
## <chr> <dbl>
## 1 a 2.67
## 2 b 3.5
I was almost right. It did not show the x column however it did show y and mean_x. Mean_x showed the mean x value of every y value. This pipeline gets the dataset, groups by y and then should the mean x value of every y value.
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
## `summarise()` has grouped output by 'y'. You can override using the `.groups`
## argument.
## # A tibble: 3 × 3
## # Groups: y [2]
## y z mean_x
## <chr> <chr> <dbl>
## 1 a K 1
## 2 a L 3.5
## 3 b K 3.5
I was wrong, the output used every combination of z and y and produced the mean_x value of that combination. The message basically says that the output is arranged by y’s but we can override it.
I believe that the output will find every combination of z and y and produce the mean_x value of that combination. Additionally it will be arranged by z instead of y.
df |>
group_by(y, z) |>
summarize(mean_x = mean(x), .groups = "drop")
## # A tibble: 3 × 3
## y z mean_x
## <chr> <chr> <dbl>
## 1 a K 1
## 2 a L 3.5
## 3 b K 3.5
I was right. The output used every combination of z and y and produced the mean_x value of that combination. The outputs are not different.
In the first output there will be 3 columns - y, z, and mean_x. The output will find every combination of z and y and produce the mean_x value of that combination. In the second output will have 4 columns - x, y, z, and mean_x. The output will find every combination of z and y and produce the mean_x value of that combination.
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
## `summarise()` has grouped output by 'y'. You can override using the `.groups`
## argument.
## # A tibble: 3 × 3
## # Groups: y [2]
## y z mean_x
## <chr> <chr> <dbl>
## 1 a K 1
## 2 a L 3.5
## 3 b K 3.5
df |>
group_by(y, z) |>
mutate(mean_x = mean(x))
## # A tibble: 5 × 4
## # Groups: y, z [3]
## x y z mean_x
## <int> <chr> <chr> <dbl>
## 1 1 a K 1
## 2 2 b K 3.5
## 3 3 a L 3.5
## 4 4 a L 3.5
## 5 5 b K 3.5
I was right, they are different because the second one is simply creating a new column on the past data frame. The first one summarizes using only the data used in the summarize.