collapse icon indicating copy to clipboard operation
collapse copied to clipboard

Graceful partial matching for column renaming

Open D3SL opened this issue 1 year ago • 1 comments

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") 
}

D3SL avatar Apr 17 '24 12:04 D3SL

Thanks. I will consider adding this, though through an extra argument as collapse does not support tidyselect expressions.

SebKrantz avatar Apr 18 '24 07:04 SebKrantz