Bobby Bobby - 1 year ago 141
R Question

Avoiding backtick characters with dplyr

How can I write the argument of

without backtick characters? I would like to do this so that I can pass in this argument from a variable as a character string.

df <- dat[["__Table"]] %>% select(`__ID` ) %>% mutate(fk_table = "__Table", val = 1)

Changing the argument of select to
gives this error:

Error: All select() inputs must resolve to integer column positions.
The following do not:
* "__ID"

Unfortunately, the
characters in column names cannot be avoided since the data is downloaded from a relational database (FileMaker) via ODBC and needs to be written back to the database while preserving the column names.

Ideally, I would like to be able to do the following:

colName <- "__ID"
df <- dat[["__Table"]] %>% select(colName) %>% mutate(fk_table = "__Table", val = 1)

I've also tried

df <- dat[["__Table"]] %>% select( eval(parse(text="__ID")) ) %>% mutate(fk_table = "__Table", val = 1)

It throws this error:

Error in parse(text = "__ID") : <text>:1:1: unexpected input
1: _

By the way, the following does work, but then I'm back to square one (still with backtick symbol).


References about backtick characters in

Answer Source

You can use with select_():

colName <- "__ID"
df <- data.frame(`__ID` = c(1,2,3), `123` = c(4,5,6), check.names = FALSE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download