Title
Problema
Cuando seleccionamos columnas usando el paquete dplyr, queremos organizar el resto de columnas al principio o el final del data frame, sin tener que escribir sus nombres manualmente.
En el siguiente ejemplo, el data frame flights tiene 19 columnas y queremos ordenar 5 de ellas al comienzo o al final del mismo.
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 
Solución
Las 5 columnas al inicio del data frame
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 
Las 5 columnas al final del data frame
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 
 
Para añadir las columnas a todo el data frame
Si quisiéramos añadir las 5 columnas, duplicándolas, a la totalidad del data frame embp.
# 5 columnas al principio
bind_cols(select(flights, one_of(col)), flights)
  # Con dplyr::relocate()
  flights %>%  
    relocate(carrier, tailnum, year, month, day) 
# 5 columnas al final
bind_cols(flights, select(flights, one_of(col)))
  # Con dplyr::relocate()
  flights %>%  
    relocate(carrier, tailnum, year, month, day, .after = last_col()) 
Entradas relacionadas
Referencias