Using the old select_()
function, I could pass a named vector into select and change position and column names at once:
my_data <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames <- c("newbar" = "bar", "newfoo" = "foo")
move_stuff <- function(df, newnames) {
select_(df, .dots = newnames)
}
move_stuff(my_data, newnames = my_newnames) )
# this is the desired output
# A tibble: 4 x 2
newbar newfoo
<int> <int>
1 10 0
2 11 1
3 12 2
4 13 3
I tried doing something similar using quosures and splicing--selecting columns works great, but the names of the vectors (and thus renaming columns at the same time) seems to be ignored. Both of the following return data frames with columns named bar
and foo
, but not newbar
and newfoo
:
move_stuff2 <- function(df, newnames) {
select(df, !!!newnames)
}
# returns df with columns bar and foo
move_stuff2(my_data, quo(my_newnames))
move_stuff2(my_data, quos(my_newnames))
Is there a way to use a named vector using the new NSE methodology to both rename and reorder columns?
quo
(orquos
for multiple) is for unquoted variable names, not strings. To convert strings to quosures usesym
(orsyms
), and use!!
or!!!
as appropriate to unquote or unquote-splice:For strings,
For unquoted variable names,