#... Logical predicates defined in terms of the variables in .data. # Multiple conditions are combined with &. # Only rows where the condition evaluates to TRUE are kept.
# A tibble: 336,776 x 19 year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> 1201311517515283081911 2201311533529485083020 3201311542540292385033 4201311544545 -110041022 -18 5201311554600 -6812837 -25 6201311554558 -474072812 7201311555600 -591385419 8201311557600 -3709723 -14 9201311557600 -3838846 -8 10201311558600 -27537458 # ... with 336,766 more rows, and 10 more variables: carrier <chr>, flight <int>, # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, # minute <dbl>, time_hour <dttm>
结合desc()函数则可实现降序排序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
> arrange(flights, desc(day))
# A tibble: 336,776 x 19 year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> 12013131121001811242225179 2201313142359545544411 3201313172359845343716 4201313112225082132785 5201313126215415232850158 620131313421591551352315140 72013131372249108132235795 820131315422501241522359113 92013131453500 -76516483 102013131522525 -38208200 # ... with 336,766 more rows, and 10 more variables: carrier <chr>, flight <int>, # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, # minute <dbl>, time_hour <dttm>
选择列 | select
select()函数可选择列名相应的列,以下返回flights数据集中的年月日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
> select(flights, year, month, day)
# A tibble: 336,776 x 3 year month day <int> <int> <int> 1201311 2201311 3201311 4201311 5201311 6201311 7201311 8201311 9201311 10201311 # ... with 336,766 more rows
还可以搭配一些辅助函数一同使用
starts_with("a") :挑选列名为“a”开头的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> flights %>% select(starts_with('a')) # A tibble: 336,776 x 3 arr_time arr_delay air_time <int> <dbl> <dbl> 183011227 285020227 392333160 41004 -18183 5812 -25116 674012150 791319158 8709 -1453 9838 -8140 107538138 # ... with 336,766 more rows
ends_with("y") : 挑选列名“y”结尾的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
> flights %>% select(ends_with('y')) # A tibble: 336,776 x 3 day dep_delay arr_delay <int> <dbl> <dbl> 11211 21420 31233 41 -1 -18 51 -6 -25 61 -412 71 -519 81 -3 -14 91 -3 -8 101 -28 # ... with 336,766 more rows