Skip to content
Prev 396586 / 398502 Next

[External] Using the pipe, |>, syntax with "names<-"

I think that the simplicity of setNames is hard to beat:

z |> setNames( c( "a", "foo" ) )

and if you are determined not to load dplyr then

column_rename <- function( DF, map ) {
  on <- names( DF )
  on[ match( map, on ) ] <- names( map )
  names( DF ) <- on
  DF
}

is more robust to column reorganization than replace():

z |> column_rename( c( foo = "b" ) )
On July 20, 2024 10:07:57 PM PDT, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote: