rename table column with metavariable

Viewed 42

I have a dataset named bridge with columns: agg, col, FACTOR. All are character values I have a variable named var which is character but contain the name of a dimension that is used in another table. here for example var <- "accounting_entry" I want to rename the column "col" of my bridge table to accounting_entry. I try this

 bridge <- bridge %>% 
   dplyr::rename(!!as.symbol(var) = col)

but i get the error message

Error: object 'ACCOUNTING_ENTRY' not found
Run `rlang::last_error()` to see where the error occurred.

Could you advice please. thanks

1 Answers

Using glue library

library(glue)
bridge <- bridge %>% 
  dplyr::rename("{var}":=col)
bridge

Also, in plain vanilla R, you can do this:

names(bridge)[names(bridge) == "col"] <- var
Related