4.2 dplyr: Columns
select(), rename(), mutate(), and relocate() keep, name, and create columns.
1. select() keeps columns
flights |> select(dep_delay, arr_delay)
## # A tibble: 336,776 × 2
## dep_delay arr_delay
## <dbl> <dbl>
## 1 2 11
## 2 4 20
## 3 2 33
## 4 -1 -18
## 5 -6 -25
## 6 -4 12
## 7 -5 19
## 8 -3 -14
## 9 -3 -8
## 10 -2 8
## # ℹ 336,766 more rows
flights |> select(year:carrier)
## # A tibble: 336,776 × 10
## 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 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # ℹ 336,766 more rows
## # ℹ 2 more variables: arr_delay <dbl>, carrier <chr>
flights |> select(-dep_delay, -arr_delay)
## # A tibble: 336,776 × 17
## year month day dep_time sched_dep_time arr_time sched_arr_time carrier
## <int> <int> <int> <int> <int> <int> <int> <chr>
## 1 2013 1 1 517 515 830 819 UA
## 2 2013 1 1 533 529 850 830 UA
## 3 2013 1 1 542 540 923 850 AA
## 4 2013 1 1 544 545 1004 1022 B6
## 5 2013 1 1 554 600 812 837 DL
## 6 2013 1 1 554 558 740 728 UA
## 7 2013 1 1 555 600 913 854 B6
## 8 2013 1 1 557 600 709 723 EV
## 9 2013 1 1 557 600 838 846 B6
## 10 2013 1 1 558 600 753 745 AA
## # ℹ 336,766 more rows
## # ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
flights |> select(-(year:day))
## # A tibble: 336,776 × 16
## dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
## <int> <int> <dbl> <int> <int> <dbl> <chr>
## 1 517 515 2 830 819 11 UA
## 2 533 529 4 850 830 20 UA
## 3 542 540 2 923 850 33 AA
## 4 544 545 -1 1004 1022 -18 B6
## 5 554 600 -6 812 837 -25 DL
## 6 554 558 -4 740 728 12 UA
## 7 555 600 -5 913 854 19 B6
## 8 557 600 -3 709 723 -14 EV
## 9 557 600 -3 838 846 -8 B6
## 10 558 600 -2 753 745 8 AA
## # ℹ 336,766 more rows
## # ℹ 9 more variables: flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
Helpers (from tidyselect) match names:
starts_with("dep")ends_with("delay")contains("arr_")matches("(.)\1")— a regular expression; we will do those in the strings weeknum_range("x", 1:3)—x1,x2,x3
flights |> select(ends_with("delay"))
## # A tibble: 336,776 × 2
## dep_delay arr_delay
## <dbl> <dbl>
## 1 2 11
## 2 4 20
## 3 2 33
## 4 -1 -18
## 5 -6 -25
## 6 -4 12
## 7 -5 19
## 8 -3 -14
## 9 -3 -8
## 10 -2 8
## # ℹ 336,766 more rows
flights |> select(starts_with("dep"), year, month, day)
## # A tibble: 336,776 × 5
## dep_time dep_delay year month day
## <int> <dbl> <int> <int> <int>
## 1 517 2 2013 1 1
## 2 533 4 2013 1 1
## 3 542 2 2013 1 1
## 4 544 -1 2013 1 1
## 5 554 -6 2013 1 1
## 6 554 -4 2013 1 1
## 7 555 -5 2013 1 1
## 8 557 -3 2013 1 1
## 9 557 -3 2013 1 1
## 10 558 -2 2013 1 1
## # ℹ 336,766 more rows
select() talks about names (or
positions). Bare names are allowed. Quoted names are also allowed:
select(flights, c("carrier", "tailnum")).
2. rename()
rename(new = old). The old name goes away.
flights |> rename(departure_time = dep_time)
## # A tibble: 336,776 × 19
## year month day departure_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ℹ 336,766 more rows
## # ℹ 12 more variables: sched_arr_time <int>, 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>
3. mutate() and transmute()
mutate() adds or overwrites columns. New columns go on
the right unless you set .before or .after.
Use = inside mutate(), not
<-.
flights |>
mutate(
gain = dep_delay - arr_delay,
speed = distance / air_time * 60
) |>
select(year:day, gain, speed)
## # A tibble: 336,776 × 5
## year month day gain speed
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 1 -9 370.
## 2 2013 1 1 -16 374.
## 3 2013 1 1 -31 408.
## 4 2013 1 1 17 517.
## 5 2013 1 1 19 394.
## 6 2013 1 1 -16 288.
## 7 2013 1 1 -24 404.
## 8 2013 1 1 11 259.
## 9 2013 1 1 5 405.
## 10 2013 1 1 -10 319.
## # ℹ 336,766 more rows
flights |>
mutate(distance = distance * 1.60934)
## # A tibble: 336,776 × 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 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # ℹ 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>
transmute() keeps only the new columns.
Nothing is saved until you assign:
flights_k <- flights |>
mutate(distance_km = distance * 1.60934)
mutate() talks about vectors. The name
dep_time means the whole column, not the string
"dep_time".
A useful conversion: dep_time is stored as 517 for 5:17.
Minutes past midnight are hours * 60 + minutes:
flights |>
mutate(
dep_h = dep_time %/% 100,
dep_m = dep_time %% 100,
dep_elapsed_min = dep_h * 60 + dep_m
)
## # A tibble: 336,776 × 22
## 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 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # ℹ 336,766 more rows
## # ℹ 14 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>, dep_h <dbl>, dep_m <dbl>,
## # dep_elapsed_min <dbl>
%/% is integer division. %% is the
remainder.
4. relocate()
Same selection language as select(). Default is the left
side. .before and .after place the block.
flights |> relocate(origin:dest)
## # A tibble: 336,776 × 19
## origin dest year month day dep_time sched_dep_time dep_delay arr_time
## <chr> <chr> <int> <int> <int> <int> <int> <dbl> <int>
## 1 EWR IAH 2013 1 1 517 515 2 830
## 2 LGA IAH 2013 1 1 533 529 4 850
## 3 JFK MIA 2013 1 1 542 540 2 923
## 4 JFK BQN 2013 1 1 544 545 -1 1004
## 5 LGA ATL 2013 1 1 554 600 -6 812
## 6 EWR ORD 2013 1 1 554 558 -4 740
## 7 EWR FLL 2013 1 1 555 600 -5 913
## 8 LGA IAD 2013 1 1 557 600 -3 709
## 9 JFK MCO 2013 1 1 557 600 -3 838
## 10 LGA ORD 2013 1 1 558 600 -2 753
## # ℹ 336,766 more rows
## # ℹ 10 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
## # flight <int>, tailnum <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm>
flights |> relocate(starts_with("d"), .after = day)
## # A tibble: 336,776 × 19
## year month day dep_time dep_delay dest distance sched_dep_time arr_time
## <int> <int> <int> <int> <dbl> <chr> <dbl> <int> <int>
## 1 2013 1 1 517 2 IAH 1400 515 830
## 2 2013 1 1 533 4 IAH 1416 529 850
## 3 2013 1 1 542 2 MIA 1089 540 923
## 4 2013 1 1 544 -1 BQN 1576 545 1004
## 5 2013 1 1 554 -6 ATL 762 600 812
## 6 2013 1 1 554 -4 ORD 719 558 740
## 7 2013 1 1 555 -5 FLL 1065 600 913
## 8 2013 1 1 557 -3 IAD 229 600 709
## 9 2013 1 1 557 -3 MCO 944 600 838
## 10 2013 1 1 558 -2 ORD 733 600 753
## # ℹ 336,766 more rows
## # ℹ 10 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
## # flight <int>, tailnum <chr>, origin <chr>, air_time <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm>
flights |> relocate(where(is.character))
## # A tibble: 336,776 × 19
## carrier tailnum origin dest year month day dep_time sched_dep_time
## <chr> <chr> <chr> <chr> <int> <int> <int> <int> <int>
## 1 UA N14228 EWR IAH 2013 1 1 517 515
## 2 UA N24211 LGA IAH 2013 1 1 533 529
## 3 AA N619AA JFK MIA 2013 1 1 542 540
## 4 B6 N804JB JFK BQN 2013 1 1 544 545
## 5 DL N668DN LGA ATL 2013 1 1 554 600
## 6 UA N39463 EWR ORD 2013 1 1 554 558
## 7 B6 N516JB EWR FLL 2013 1 1 555 600
## 8 EV N829AS LGA IAD 2013 1 1 557 600
## 9 B6 N593JB JFK MCO 2013 1 1 557 600
## 10 AA N3ALAA LGA ORD 2013 1 1 558 600
## # ℹ 336,766 more rows
## # ℹ 10 more variables: dep_delay <dbl>, arr_time <int>, sched_arr_time <int>,
## # arr_delay <dbl>, flight <int>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm>
5. Practice
- Keep
originanddesttwo ways. - Keep every arrival column plus
year,month, andday. Use as few characters as you can. - Convert
dep_timeandsched_dep_timeto minutes past midnight. Assign the result if you want to keep it. - Move columns that end with
"time"to just beforeflight. Then put theschedcolumns before the actual times.