美文网首页
学习tidyverse - 数据转换(2)

学习tidyverse - 数据转换(2)

作者: DumplingLucky | 来源:发表于2022-02-20 15:14 被阅读0次

    可视化是一个重要工具,但是我们需要把数据整理成正确的形式来进行可视化。 通常,需要创建一些新的变量或摘要,或者重命名变量或对观察值进行重新排序,以使数据可视化起来更容易一些。 这里主要会使用到dplyr包,涉及到mutate()函数。

    1. 使用mutate()添加新的变量

    除了选择现有列之外,mutate()可以实现添加新列的功能。

    mutate()在数据集的末尾添加新列,首先创建一个窄的数据集,方便看到新的变量。

    flights_sml <- select(flights, 
      year:day, 
      ends_with("delay"), 
      distance, 
      air_time
    )
    mutate(flights_sml,
      gain = dep_delay - arr_delay,
      speed = distance / air_time * 60
    )
    #> # A tibble: 336,776 x 9
    #>    year month   day dep_delay arr_delay distance air_time  gain speed
    #>   <int> <int> <int>     <dbl>     <dbl>    <dbl>    <dbl> <dbl> <dbl>
    #> 1  2013     1     1         2        11     1400      227    -9  370.
    #> 2  2013     1     1         4        20     1416      227   -16  374.
    #> 3  2013     1     1         2        33     1089      160   -31  408.
    #> 4  2013     1     1        -1       -18     1576      183    17  517.
    #> 5  2013     1     1        -6       -25      762      116    19  394.
    #> 6  2013     1     1        -4        12      719      150   -16  288.
    #> # … with 336,770 more rows
    
    mutate(flights_sml,
      gain = dep_delay - arr_delay,
      hours = air_time / 60,
      gain_per_hour = gain / hours
    )
    #> # A tibble: 336,776 x 10
    #>    year month   day dep_delay arr_delay distance air_time  gain hours
    #>   <int> <int> <int>     <dbl>     <dbl>    <dbl>    <dbl> <dbl> <dbl>
    #> 1  2013     1     1         2        11     1400      227    -9  3.78
    #> 2  2013     1     1         4        20     1416      227   -16  3.78
    #> 3  2013     1     1         2        33     1089      160   -31  2.67
    #> 4  2013     1     1        -1       -18     1576      183    17  3.05
    #> 5  2013     1     1        -6       -25      762      116    19  1.93
    #> 6  2013     1     1        -4        12      719      150   -16  2.5 
    #> # … with 336,770 more rows, and 1 more variable: gain_per_hour <dbl>
    

    如果只想保留新变量,可以使用transmute()

    transmute(flights,
      gain = dep_delay - arr_delay,
      hours = air_time / 60,
      gain_per_hour = gain / hours
    )
    #> # A tibble: 336,776 x 3
    #>    gain hours gain_per_hour
    #>   <dbl> <dbl>         <dbl>
    #> 1    -9  3.78         -2.38
    #> 2   -16  3.78         -4.23
    #> 3   -31  2.67        -11.6 
    #> 4    17  3.05          5.57
    #> 5    19  1.93          9.83
    #> 6   -16  2.5          -6.4 
    #> # … with 336,770 more rows
    

    有许多用于创建新变量的函数,可以将它们与mutate()一起使用。必须对函数进行向量化:它必须将向量作为输入,并返回具有与输出相同数量的向量。在航班数据集中,可以使用以下命令从dep_time计算小时和分钟:

    transmute(flights,
      dep_time,
      hour = dep_time %/% 100,
      minute = dep_time %% 100
    )
    #> # A tibble: 336,776 x 3
    #>   dep_time  hour minute
    #>      <int> <dbl>  <dbl>
    #> 1      517     5     17
    #> 2      533     5     33
    #> 3      542     5     42
    #> 4      544     5     44
    #> 5      554     5     54
    #> 6      554     5     54
    #> # … with 336,770 more rows
    
    (x <- 1:10)
    #>  [1]  1  2  3  4  5  6  7  8  9 10
    lag(x)
    #>  [1] NA  1  2  3  4  5  6  7  8  9
    lead(x)
    #>  [1]  2  3  4  5  6  7  8  9 10 NA
    

    累积和滚动聚合:R提供用于运行总和,乘积,最小值和最大值的函数:cumsum(),cumprod(),cummin(),cummax(); dplyr提供cummean()来累积平均值。

    x
    #>  [1]  1  2  3  4  5  6  7  8  9 10
    cumsum(x)
    #>  [1]  1  3  6 10 15 21 28 36 45 55
    cummean(x)
    #>  [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5
    

    逻辑比较,<,<=,>,> =,!===
    排名:有许多排名函数,min_rank()使用最常用的排名类型(例如,第一,第二,第二,第四
    ),降序时使用desc(x),该函数返回元素的秩。

    y <- c(1, 2, 2, NA, 3, 4)
    min_rank(y)
    #> [1]  1  2  2 NA  4  5
    min_rank(desc(y))
    #> [1]  5  3  3 NA  2  1
    

    还有其他用于排序的变体row_number(),densed_rank(),percent_rank(),cume_dist()ntile()

    row_number(y)
    #> [1]  1  2  3 NA  4  5
    dense_rank(y)
    #> [1]  1  2  2 NA  3  4
    percent_rank(y)
    #> [1] 0.00 0.25 0.25   NA 0.75 1.00
    cume_dist(y)
    #> [1] 0.2 0.6 0.6  NA 0.8 1.0
    

    参考:https://r4ds.had.co.nz/transform.html

    相关文章

      网友评论

          本文标题:学习tidyverse - 数据转换(2)

          本文链接:https://www.haomeiwen.com/subject/xqhvlrtx.html