美文网首页
R函数学习-mutate()

R函数学习-mutate()

作者: Thinkando | 来源:发表于2020-03-27 13:49 被阅读0次

    mutate() 给原来的数据框添加新的列,一般添加在后面

    flights_sml <- select(flights,
                          year:day,
                          ends_with("delay"),
                          distance,
                          air_time)
    
    mutate(flights_sml,
           gain = arr_delay - dep_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.00     11.0      1400    227     9.00   370
    ##  2  2013     1     1      4.00     20.0      1416    227    16.0    374
    ##  3  2013     1     1      2.00     33.0      1089    160    31.0    408
    ##  4  2013     1     1     -1.00    -18.0      1576    183   -17.0    517
    ##  5  2013     1     1     -6.00    -25.0       762    116   -19.0    394
    ##  6  2013     1     1     -4.00     12.0       719    150    16.0    288
    ##  7  2013     1     1     -5.00     19.0      1065    158    24.0    404
    ##  8  2013     1     1     -3.00    -14.0       229     53.0 -11.0    259
    ##  9  2013     1     1     -3.00    - 8.00      944    140   - 5.00   405
    ## 10  2013     1     1     -2.00      8.00      733    138    10.0    319
    ## # ... with 336,766 more rows
    
    # 上面的代码可改写成
    df <- flights_sml %>% mutate(gain = arr_delay - dep_delay,
           speed = distance / air_time * 60)
    

    相关文章

      网友评论

          本文标题:R函数学习-mutate()

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