tidyverse初认识

1 第一题 编写代码

利用nycflights13包的flights数据集是2013年从纽约三大机场(JFK、LGA、EWR)起飞的所有航班的准点数据,共336776条记录。

  • 计算纽约三大机场2013起飞航班数和平均延误时间(可使用group_by, summarise函数)

    install.packages("nycflights13")
    Warning: package 'nycflights13' is in use and will not be installed
    install.packages("tidyverse")
    Warning: package 'tidyverse' is in use and will not be installed
    library("nycflights13")
    data(package = "nycflights13")
    data("flights")
    head(flights)
    # A tibble: 6 × 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
    # ℹ 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 |> 
      group_by(origin) |> 
      summarise(n=n(),dly=mean(dep_delay,na.rm =T))
    # A tibble: 3 × 3
      origin      n   dly
      <chr>   <int> <dbl>
    1 EWR    120835  15.1
    2 JFK    111279  12.1
    3 LGA    104662  10.3
  • 计算不同航空公司2013从纽约起飞航班数和平均延误时间

    flights |> 
      group_by(carrier) |> 
      summarise(n=n(),dly=mean(dep_delay,na.rm =T)) |> 
      arrange(desc(n))
    # A tibble: 16 × 3
       carrier     n   dly
       <chr>   <int> <dbl>
     1 UA      58665 12.1 
     2 B6      54635 13.0 
     3 EV      54173 20.0 
     4 DL      48110  9.26
     5 AA      32729  8.59
     6 MQ      26397 10.6 
     7 US      20536  3.78
     8 9E      18460 16.7 
     9 WN      12275 17.7 
    10 VX       5162 12.9 
    11 FL       3260 18.7 
    12 AS        714  5.80
    13 F9        685 20.2 
    14 YV        601 19.0 
    15 HA        342  4.90
    16 OO         32 12.6 
  • 计算纽约三大机场排名前三个目的地和平均飞行距离(可使用group_by, summarise, arrange, slice_max函数)

    flights |> 
      group_by(origin,dest) |> 
      summarise(n=n(),dist=mean(distance)) |> 
      slice_max(n,n=3)
    `summarise()` has grouped output by 'origin'. You can override using the
    `.groups` argument.
    # A tibble: 9 × 4
    # Groups:   origin [3]
      origin dest      n  dist
      <chr>  <chr> <int> <dbl>
    1 EWR    ORD    6100   719
    2 EWR    BOS    5327   200
    3 EWR    SFO    5127  2565
    4 JFK    LAX   11262  2475
    5 JFK    SFO    8204  2586
    6 JFK    BOS    5898   187
    7 LGA    ATL   10263   762
    8 LGA    ORD    8857   733
    9 LGA    CLT    6168   544

2 第二题 解释代码

  1. 代码含义:将iris数据集转换为tibble格式后,首先按 Species 列升序排列,在每个 Species 组内,对 Sepal.Length 和 Sepal.Width 列进行降序排列。

    tibble(iris) %>% 
      arrange(Species,across(starts_with("Sepal"), desc))
    # A tibble: 150 × 5
       Sepal.Length Sepal.Width Petal.Length Petal.Width Species
              <dbl>       <dbl>        <dbl>       <dbl> <fct>  
     1          5.8         4            1.2         0.2 setosa 
     2          5.7         4.4          1.5         0.4 setosa 
     3          5.7         3.8          1.7         0.3 setosa 
     4          5.5         4.2          1.4         0.2 setosa 
     5          5.5         3.5          1.3         0.2 setosa 
     6          5.4         3.9          1.7         0.4 setosa 
     7          5.4         3.9          1.3         0.4 setosa 
     8          5.4         3.7          1.5         0.2 setosa 
     9          5.4         3.4          1.7         0.2 setosa 
    10          5.4         3.4          1.5         0.4 setosa 
    # ℹ 140 more rows
  2. 代码含义:加载数据集starwars,并按照gender进行分组,通过filter进行数据的过滤,筛选出每个性别组中体重(mass)大于该组平均体重的角色。mean用于计算数据的平均值,na.rm=True表示忽略mass列中的缺失值(NA)。

    starwars %>% 
      group_by(gender) %>% 
      filter(mass > mean(mass, na.rm = TRUE))
    # A tibble: 15 × 14
    # Groups:   gender [3]
       name    height   mass hair_color skin_color eye_color birth_year sex   gender
       <chr>    <int>  <dbl> <chr>      <chr>      <chr>          <dbl> <chr> <chr> 
     1 Darth …    202  136   none       white      yellow          41.9 male  mascu…
     2 Owen L…    178  120   brown, gr… light      blue            52   male  mascu…
     3 Beru W…    165   75   brown      light      blue            47   fema… femin…
     4 Chewba…    228  112   brown      unknown    blue           200   male  mascu…
     5 Jabba …    175 1358   <NA>       green-tan… orange         600   herm… mascu…
     6 Jek To…    180  110   brown      fair       blue            NA   <NA>  <NA>  
     7 IG-88      200  140   none       metal      red             15   none  mascu…
     8 Bossk      190  113   none       green      red             53   male  mascu…
     9 Ayla S…    178   55   none       blue       hazel           48   fema… femin…
    10 Gregar…    185   85   black      dark       brown           NA   <NA>  <NA>  
    11 Lumina…    170   56.2 black      yellow     blue            58   fema… femin…
    12 Zam We…    168   55   blonde     fair, gre… yellow          NA   fema… femin…
    13 Shaak …    178   57   none       red, blue… black           NA   fema… femin…
    14 Grievo…    216  159   none       brown, wh… green, y…       NA   male  mascu…
    15 Tarfful    234  136   brown      brown      blue            NA   male  mascu…
    # ℹ 5 more variables: homeworld <chr>, species <chr>, films <list>,
    #   vehicles <list>, starships <list>
  3. 代码含义:加载数据集starwars,从数据集中选择name, homeworld, species这三列,除去name列,将homeworld和species列的数据类型转换为因子(factor),而name列保持不变。mutate表示对数据框进行列操作,表示在原有的基础上添加或者修改列。

    starwars %>%
      select(name, homeworld, species) %>%
      mutate(across(!name, as.factor))
    # A tibble: 87 × 3
       name               homeworld species
       <chr>              <fct>     <fct>  
     1 Luke Skywalker     Tatooine  Human  
     2 C-3PO              Tatooine  Droid  
     3 R2-D2              Naboo     Droid  
     4 Darth Vader        Tatooine  Human  
     5 Leia Organa        Alderaan  Human  
     6 Owen Lars          Tatooine  Human  
     7 Beru Whitesun Lars Tatooine  Human  
     8 R5-D4              Tatooine  Droid  
     9 Biggs Darklighter  Tatooine  Human  
    10 Obi-Wan Kenobi     Stewjon   Human  
    # ℹ 77 more rows
  4. 代码含义:将mtcars数据集转换为tibble格式,并按照vs列进行分组。在每组中,将 hp 列的值分成 3 个等宽的区间,并创建新列hp_cut。最后,按 hp_cut 列重新分组。

    tibble(mtcars) %>%
      group_by(vs) %>%
      mutate(hp_cut = cut(hp, 3)) %>%
      group_by(hp_cut)
    # A tibble: 32 × 12
    # Groups:   hp_cut [6]
         mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb hp_cut     
       <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct>      
     1  21       6  160    110  3.9   2.62  16.5     0     1     4     4 (90.8,172] 
     2  21       6  160    110  3.9   2.88  17.0     0     1     4     4 (90.8,172] 
     3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1 (75.7,99.3]
     4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1 (99.3,123] 
     5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2 (172,254]  
     6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1 (99.3,123] 
     7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4 (172,254]  
     8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2 (51.9,75.7]
     9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2 (75.7,99.3]
    10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4 (99.3,123] 
    # ℹ 22 more rows

3 第三题 查找帮助理解函数

阅读 https://dplyr.tidyverse.org/reference/mutate-joins.html 内容,说明4个数据集链接函数函数的作用。分别举一个实际例子演示并解释其输出结果。

  1. inner_join() :只保留两个数据集中键匹配的行

    library(dplyr)
    
    # 创建两个示例数据集
    iris1 <- iris %>% select(Sepal.Length, Sepal.Width, Species) %>% slice(1:5)
    iris2 <- iris %>% select(Sepal.Length, Petal.Length, Species) %>% slice(3:7)
    
    print("iris1:")
    [1] "iris1:"
    print(iris1)
      Sepal.Length Sepal.Width Species
    1          5.1         3.5  setosa
    2          4.9         3.0  setosa
    3          4.7         3.2  setosa
    4          4.6         3.1  setosa
    5          5.0         3.6  setosa
    print("iris2:")
    [1] "iris2:"
    print(iris2)
      Sepal.Length Petal.Length Species
    1          4.7          1.3  setosa
    2          4.6          1.5  setosa
    3          5.0          1.4  setosa
    4          5.4          1.7  setosa
    5          4.6          1.4  setosa
    result_inner <- inner_join(iris1, iris2, by = c("Sepal.Length", "Species"))
    print("inner_join 结果:")
    [1] "inner_join 结果:"
    print(result_inner)
      Sepal.Length Sepal.Width Species Petal.Length
    1          4.7         3.2  setosa          1.3
    2          4.6         3.1  setosa          1.5
    3          4.6         3.1  setosa          1.4
    4          5.0         3.6  setosa          1.4
  2. left_join():保留左侧数据集中的所有行,右侧数据集中没有匹配的键时填充 NA。

    result_left <- left_join(iris1, iris2, by = c("Sepal.Length", "Species"))
    print("left_join 结果:")
    [1] "left_join 结果:"
    print(result_left)
      Sepal.Length Sepal.Width Species Petal.Length
    1          5.1         3.5  setosa           NA
    2          4.9         3.0  setosa           NA
    3          4.7         3.2  setosa          1.3
    4          4.6         3.1  setosa          1.5
    5          4.6         3.1  setosa          1.4
    6          5.0         3.6  setosa          1.4
  3. right_join():保留右侧数据集中的所有行,左侧数据集中没有匹配的键时填充 NA。

    result_right <- right_join(iris1, iris2, by = c("Sepal.Length", "Species"))
    print("right_join 结果:")
    [1] "right_join 结果:"
    print(result_right)
      Sepal.Length Sepal.Width Species Petal.Length
    1          4.7         3.2  setosa          1.3
    2          4.6         3.1  setosa          1.5
    3          4.6         3.1  setosa          1.4
    4          5.0         3.6  setosa          1.4
    5          5.4          NA  setosa          1.7
  4. full_join():保留两个数据集中的所有行,没有匹配的键时填充 NA。

    result_full <- full_join(iris1, iris2, by = c("Sepal.Length", "Species"))
    print("full_join 结果:")
    [1] "full_join 结果:"
    print(result_full)
      Sepal.Length Sepal.Width Species Petal.Length
    1          5.1         3.5  setosa           NA
    2          4.9         3.0  setosa           NA
    3          4.7         3.2  setosa          1.3
    4          4.6         3.1  setosa          1.5
    5          4.6         3.1  setosa          1.4
    6          5.0         3.6  setosa          1.4
    7          5.4          NA  setosa          1.7