collapse
collapse copied to clipboard
Graceful partial matching for column renaming
Regex support in get_vars() trivializes dealing with optional columns, but there's no equivalent to renaming afaik other than strings of if blocks. An option to ignore missing columns while renaming would mimic tidyselect's any_of and replace a great deal of boilerplate and repeated code.
eg:
library(tidyverse)
library(collapse)
sample<-data.frame(
foo="foo",
bar="bar",
foobar="foobar"
)
sample<-sample |> rename(any_of(c(both="foobar",both="foo_bar")))
##contrast with
if("foobar" %chin% names(sample)){
sample<-sample |> frename(both="foobar")
}
if("foo_bar" %chin% names(sample)){
sample<-sample |> frename(both="foo_bar")
}
Thanks. I will consider adding this, though through an extra argument as collapse does not support tidyselect expressions.