応用計量経済分析演習(PBL) 課題02

作者

j324247 平松瑠希

公開

2026年9月16日

library(nycflights13)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── 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

(1)flightsの表示

flights
# 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>

(2)glimpse()との表示の違い

glimpse(flights)
Rows: 336,776
Columns: 19
$ year           <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2…
$ month          <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ day            <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ dep_time       <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 558, 558, …
$ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 600, 600, …
$ dep_delay      <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2, -2, -1…
$ arr_time       <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 753, 849,…
$ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 745, 851,…
$ arr_delay      <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -3, 7, -1…
$ carrier        <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV", "B6", "…
$ flight         <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79, 301, 4…
$ tailnum        <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN", "N394…
$ origin         <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR", "LGA",…
$ dest           <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL", "IAD",…
$ air_time       <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138, 149, 1…
$ distance       <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 944, 733, …
$ hour           <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6…
$ minute         <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0…
$ time_hour      <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013-01-01 0…

flights はデータを表として表示し、先頭の行と、表示幅に収まる列を示す。表示しきれない行や列は省略される。

一方、glimpse(flights) は列ごとに1行を使い、全列の名前、型、先頭の値を表示する。どちらもデータ全体の行数と列数を示すが、glimpse() では列の構成を一覧で確認できる。

(3)条件に合うフライトの抽出

(a)1月1日に出発したフライト

flights_jan1 <- flights |>
  filter(month == 1, day == 1)
flights_jan1
# A tibble: 842 × 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
# ℹ 832 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>

該当するのは 842 件である。

(b)出発が2時間以上遅れたフライト

出発遅延時間 dep_delay の単位は分なので、120分以上を条件にする。

flights_delayed <- flights |>
  filter(dep_delay >= 120)
flights_delayed
# A tibble: 9,888 × 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      848           1835       853     1001           1950
 2  2013     1     1      957            733       144     1056            853
 3  2013     1     1     1114            900       134     1447           1222
 4  2013     1     1     1540           1338       122     2020           1825
 5  2013     1     1     1815           1325       290     2120           1542
 6  2013     1     1     1842           1422       260     1958           1535
 7  2013     1     1     1856           1645       131     2212           2005
 8  2013     1     1     1934           1725       129     2126           1855
 9  2013     1     1     1938           1703       155     2109           1823
10  2013     1     1     1942           1705       157     2124           1830
# ℹ 9,878 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>

該当するのは 9888 件である。出発遅延時間が欠損している行は条件を判定できないため、抽出結果には含まれない。

(c)1月1日に出発し、出発が2時間以上遅れたフライト

flights_jan1_delayed <- flights |>
  filter(month == 1, day == 1, dep_delay >= 120)
flights_jan1_delayed
# A tibble: 17 × 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      848           1835       853     1001           1950
 2  2013     1     1      957            733       144     1056            853
 3  2013     1     1     1114            900       134     1447           1222
 4  2013     1     1     1540           1338       122     2020           1825
 5  2013     1     1     1815           1325       290     2120           1542
 6  2013     1     1     1842           1422       260     1958           1535
 7  2013     1     1     1856           1645       131     2212           2005
 8  2013     1     1     1934           1725       129     2126           1855
 9  2013     1     1     1938           1703       155     2109           1823
10  2013     1     1     1942           1705       157     2124           1830
11  2013     1     1     2006           1630       216     2230           1848
12  2013     1     1     2009           1808       121     2145           1942
13  2013     1     1     2115           1700       255     2330           1920
14  2013     1     1     2205           1720       285       46           2040
15  2013     1     1     2221           2000       141     2331           2124
16  2013     1     1     2312           2000       192       21           2110
17  2013     1     1     2343           1724       379      314           1938
# ℹ 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>

該当するのは 17 件である。

(d)到着空港がヒューストン市内のフライト

flights_houston <- flights |>
  filter(dest == "IAH" | dest == "HOU")
flights_houston
# 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>

該当するのは 9313 件である。

(4)並べ替えと列の移動

(a)距離の降順

flights_distance <- flights |>
  arrange(desc(distance)) |>
  relocate(distance, .before = 1)
flights_distance
# A tibble: 336,776 × 19
   distance  year month   day dep_time sched_dep_time dep_delay arr_time
      <dbl> <int> <int> <int>    <int>          <int>     <dbl>    <int>
 1     4983  2013     1     1      857            900        -3     1516
 2     4983  2013     1     2      909            900         9     1525
 3     4983  2013     1     3      914            900        14     1504
 4     4983  2013     1     4      900            900         0     1516
 5     4983  2013     1     5      858            900        -2     1519
 6     4983  2013     1     6     1019            900        79     1558
 7     4983  2013     1     7     1042            900       102     1620
 8     4983  2013     1     8      901            900         1     1504
 9     4983  2013     1     9      641            900      1301     1242
10     4983  2013     1    10      859            900        -1     1449
# ℹ 336,766 more rows
# ℹ 11 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
#   flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#   hour <dbl>, minute <dbl>, time_hour <dttm>

(b)到着遅延時間の降順

flights_arr_delay <- flights |>
  arrange(desc(arr_delay)) |>
  relocate(arr_delay, .before = 1)
flights_arr_delay
# A tibble: 336,776 × 19
   arr_delay  year month   day dep_time sched_dep_time dep_delay arr_time
       <dbl> <int> <int> <int>    <int>          <int>     <dbl>    <int>
 1      1272  2013     1     9      641            900      1301     1242
 2      1127  2013     6    15     1432           1935      1137     1607
 3      1109  2013     1    10     1121           1635      1126     1239
 4      1007  2013     9    20     1139           1845      1014     1457
 5       989  2013     7    22      845           1600      1005     1044
 6       931  2013     4    10     1100           1900       960     1342
 7       915  2013     3    17     2321            810       911      135
 8       895  2013     7    22     2257            759       898      121
 9       878  2013    12     5      756           1700       896     1058
10       875  2013     5     3     1133           2055       878     1250
# ℹ 336,766 more rows
# ℹ 11 more variables: sched_arr_time <int>, carrier <chr>, flight <int>,
#   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#   hour <dbl>, minute <dbl>, time_hour <dttm>

到着遅延時間が欠損している行は末尾に並ぶ。

(5)月ごとの総フライト数と平均到着遅延時間

総フライト数は各月の全行を数える。平均到着遅延時間は、設問の指示に従って欠損値を除いて計算する。

monthly_summary <- flights |>
  group_by(month) |>
  summarise(
    total_flights = n(),
    mean_arr_delay = mean(arr_delay, na.rm = TRUE),
    .groups = "drop"
  ) |>
  arrange(month)

knitr::kable(
  monthly_summary,
  digits = 2,
  col.names = c("月", "総フライト数", "平均到着遅延時間(分)")
)
月 総フライト数 平均到着遅延時間(分)
1 27004 6.13
2 24951 5.61
3 28834 5.81
4 28330 11.18
5 28796 3.52
6 28243 16.48
7 29425 16.71
8 29327 6.04
9 27574 -4.02
10 28889 -0.17
11 27268 0.46
12 28135 14.87

平均到着遅延時間は表示時に小数第2位まで丸めた。負の値は、平均すると予定時刻より早く到着したことを表す。