很少有数据分析仅涉及单个数据表。通常,有许多数据表,必须将它们组合起来才能回答问题。总的来说,多个数据表称为关系数据,因为重要的是关系,而不仅仅是单个数据集。
我们将使用dplyr的两表动词来探索
nycflights13
中的关系数据。
library(tidyverse)
library(nycflights13)
1. nycflights13
我们将使用
nycflights13
包来学习关系数据。
nycflights13
包含四个在数据转换中使用的
flights
表相关的小标题:
airlines
#> # A tibble: 16 x 2
#> carrier name
#> <chr> <chr>
#> 1 9E Endeavor Air Inc.
#> 2 AA American Airlines Inc.
#> 3 AS Alaska Airlines Inc.
#> 4 B6 JetBlue Airways
#> 5 DL Delta Air Lines Inc.
#> 6 EV ExpressJet Airlines Inc.
#> # … with 10 more rows
airports
提供了有关每个机场的信息,由
faa
机场代码标识:
airports
#> # A tibble: 1,458 x 8
#> faa name lat lon alt tz dst tzone
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
#> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/New_Y…
#> 2 06A Moton Field Municipal Airp… 32.5 -85.7 264 -6 A America/Chica…
#> 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/Chica…
#> 4 06N Randall Airport 41.4 -74.4 523 -5 A America/New_Y…
#> 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/New_Y…
#> 6 0A9 Elizabethton Municipal Air… 36.4 -82.2 1593 -5 A America/New_Y…
#> # … with 1,452 more rows
planes
提供有关每个平面的信息,由
tailnum
标识:
planes
#> # A tibble: 3,322 x 9
#> tailnum year type manufacturer model engines seats speed engine
#> <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
#> 1 N10156 2004 Fixed wing mu… EMBRAER EMB-1… 2 55 NA Turbo-…
#> 2 N102UW 1998 Fixed wing mu… AIRBUS INDUST… A320-… 2 182 NA Turbo-…
#> 3 N103US 1999 Fixed wing mu… AIRBUS INDUST… A320-… 2 182 NA Turbo-…
#> 4 N104UW 1999 Fixed wing mu… AIRBUS INDUST… A320-… 2 182 NA Turbo-…
#> 5 N10575 2002 Fixed wing mu… EMBRAER EMB-1… 2 55 NA Turbo-…
#> 6 N105UW 1999 Fixed wing mu… AIRBUS INDUST… A320-… 2 182 NA Turbo-…
#> # … with 3,316 more rows
weather
给出每个纽约市机场每小时的天气情况:
weather
#> # A tibble: 26,115 x 15
#> origin year month day hour temp dewp humid wind_dir wind_speed wind_gust
#> <chr> <int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 EWR 2013 1 1 1 39.0 26.1 59.4 270 10.4 NA
#> 2 EWR 2013 1 1 2 39.0 27.0 61.6 250 8.06 NA
#> 3 EWR 2013 1 1 3 39.0 28.0 64.4 240 11.5 NA
#> 4 EWR 2013 1 1 4 39.9 28.0 62.2 250 12.7 NA
#> 5 EWR 2013 1 1 5 39.0 28.0 64.4 260 12.7 NA
#> 6 EWR 2013 1 1 6 37.9 28.0 67.2 240 11.5 NA
#> # … with 26,109 more rows, and 4 more variables: precip <dbl>, pressure <dbl>,
#> # visib <dbl>, time_hour <dttm>
显示不同表之间关系的一种方法是使用图形:
对于
nycflights13
:
-
flights通过一个单一变量tailnum连接到planes.
-
flights通过carrier变量连接到airlines.
-
flights通过origin和dest变量连接到airports.
-
flights通过origin (the location), and year, month, day和hour (the time)连接到weather.
2. Keys
用于连接每对表的变量称为键。键是唯一标识观察值的变量(或变量集)。在简单的情况下,单个变量足以识别观察结果。例如,每个平面由其尾号唯一标识。在其他情况下,可能需要多个变量。例如,要确定天气中的观测值,需要五个变量:年,月,日,小时和原点。
有两种类型的键:
主键
primary key
在其自己的表中唯一标识观察值。例如,planes $ tailnum是主键,因为它唯一地标识了planes表中的每个平面。
外键
foreign key
唯一地标识另一个表中的观察值。例如,flights $ tailnum是一个外键,因为它出现在flights表中,它使每个航班与唯一的飞机匹配。
变量既可以是主键,也可以是外键。例如,起源是天气主键的一部分,也是机场表的外键。
确定表中的主键后,最好验证一下它们是否确实可以唯一地标识每个观察值。一种方法是对主键进行
count()
并查找
n
大于1的条目:
planes %>%
count(tailnum) %>%
filter(n > 1)
#> # A tibble: 0 x 2
#> # … with 2 variables: tailnum <chr>, n <int>
weather %>%
count(year, month, day, hour, origin) %>%
filter(n > 1)
#> # A tibble: 3 x 6
#> year month day hour origin n
#> <int> <int> <int> <int> <chr> <int>
#> 1 2013 11 3 1 EWR 2
#> 2 2013 11 3 1 JFK 2
#> 3 2013 11 3 1 LGA 2
有时表没有明确的主键:每一行都是观察值,但是没有变量组合可靠地标识它。 例如,排期表中的主键是什么? 您可能会认为这是日期加上航班号或机尾号,但是这两个都不是唯一的:
flights %>%
count(year, month, day, flight) %>%
filter(n > 1)
#> # A tibble: 29,768 x 5
#> year month day flight n
#> <int> <int> <int> <int> <int>
#> 1 2013 1 1 1 2
#> 2 2013 1 1 3 2
#> 3 2013 1 1 4 2
#> 4 2013 1 1 11 3
#> 5 2013 1 1 15 2
#> 6 2013 1 1 21 2
#> # … with 29,762 more rows
flights %>%
count(year, month, day, tailnum) %>%
filter(n > 1)
#> # A tibble: 64,928 x 5
#> year month day tailnum n
#> <int> <int> <int> <chr> <int>
#> 1 2013 1 1 N0EGMQ 2
#> 2 2013 1 1 N11189 2
#> 3 2013 1 1 N11536 2
#> 4 2013 1 1 N11544 3
#> 5 2013 1 1 N11551 2
#> 6 2013 1 1 N12540 2
#> # … with 64,922 more rows
参考:
https://r4ds.had.co.nz/relational-data.html