r - dplyr::select - Including All Other Columns at End of New Data Frame (or Beginning or Middle) -
when interacting data find dplyr library's select() function great way organize data frame columns.
one great use, if happen working df has many columns, find myself putting 2 variables next each other easy comparison. when doing this, need attached other columns either before or after. found matches(".")
function super convenient way this.
for example:
library(nycflights13) library(dplyr) # have 5 columns: select(flights, carrier, tailnum, year, month, day) # new order column: select(flights, carrier, tailnum, year, month, day, matches(".")) # matches(".") attached other columns end of new data frame
the question - curious if there better way this? better in sense of being more flexible.
for example of 1 issue: there way include "all other" columns @ beginning or middle of new data.frame? (note select(flights, matches("."), year, month, day, )
doesn't produce desired result, since matches(".")
attached columns , year, month, day
ignored because repeats of existing columns names.)
if want reorder columns
select(flights, carrier, tailnum, year, month, day, everything())
or in 2 steps, select variables provided in character vector, one_of("x", "y", "z")
:
col <- c("carrier", "tailnum", "year", "month", "day") select(flights, one_of(col), everything())
select(flights, -one_of(col), one_of(col))
if want add data frame again using
dplyr
:
bind_cols(select(flights, one_of(col)), flights)
bind_cols(flights, select(flights, one_of(col)))
Comments
Post a Comment