Problem
In dplyr when using select, we want to reorder all other columns at the beginning or end of a data frame without having to type these column names again. In the following example, the data drame flights has 19 columns and we want to order 5 of them at the beginning or end of a data frame.
library(nycflights13)
library(dplyr)
head(flights)
# A tibble: 6 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
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
# ... with 11 more variables: arr_delay , carrier , flight ,
# tailnum , origin , dest , air_time , distance ,
# hour , minute , time_hour
Solution
col <- c("carrier", "tailnum", "year", "month", "day")
select(flights, one_of(col), everything())
# A tibble: 336,776 × 19
carrier tailnum year month day dep_time sched_dep_time dep_delay arr_time
1 UA N14228 2013 1 1 517 515 2 830
2 UA N24211 2013 1 1 533 529 4 850
3 AA N619AA 2013 1 1 542 540 2 923
4 B6 N804JB 2013 1 1 544 545 -1 1004
5 DL N668DN 2013 1 1 554 600 -6 812
6 UA N39463 2013 1 1 554 558 -4 740
7 B6 N516JB 2013 1 1 555 600 -5 913
8 EV N829AS 2013 1 1 557 600 -3 709
9 B6 N593JB 2013 1 1 557 600 -3 838
10 AA N3ALAA 2013 1 1 558 600 -2 753
# ... with 336,766 more rows, and 10 more variables: sched_arr_time ,
# arr_delay , flight , origin , dest , air_time ,
# distance , hour , minute , time_hour
select(flights, -one_of(col), one_of(col))
# A tibble: 336,776 × 19
dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay flight
1 517 515 2 830 819 11 1545
2 533 529 4 850 830 20 1714
3 542 540 2 923 850 33 1141
4 544 545 -1 1004 1022 -18 725
5 554 600 -6 812 837 -25 461
6 554 558 -4 740 728 12 1696
7 555 600 -5 913 854 19 507
8 557 600 -3 709 723 -14 5708
9 557 600 -3 838 846 -8 79
10 558 600 -2 753 745 8 301
# ... with 336,766 more rows, and 12 more variables: origin , dest ,
# air_time , distance , hour , minute , time_hour ,
# carrier , tailnum , year , month , day
In this case we want to add all the data frame at the beginning or the end again, resulting in duplicates of our 5 columns.
# 5 columns at the beginning
bind_cols(select(flights, one_of(col)), flights)
# 5 columns at the end
bind_cols(flights, select(flights, one_of(col)))
References
No hay comentarios:
Publicar un comentario