避免使用dplyr的反向字符 [英] Avoiding backtick characters with dplyr

查看:134
本文介绍了避免使用dplyr的反向字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写的参数,选择而不使用反号码字符?我想这样做,以便我可以从一个变量作为一个字符串传入这个参数。

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

将选择的参数更改为__ ID给出了此错误:

 错误:所有select()输入必须解析为整数列位置。 
以下不要:
*__ID

不幸的是, code> _ 不能避免列名中的字符,因为数据是通过ODBC从关系数据库(FileMaker)下载的,并且需要在保留列名的同时将其写回数据库。 / p>

理想情况下,我希望能够执行以下操作:

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

我也尝试过 eval(parse())

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

它抛出这个错误:

 解析错误(text =__ID):< text>:1:1:意外输入
1:_
^

顺便说一句,工作,但然后我回到了一个方块(仍然有反向符号)。

  eval(parse(text =`__ID )

关于 R中的反引号字符的引用




解决方案

您可以使用 as.name() with select _()

  colName<  - __ID
df< - data.frame(`__ID` = c(1,2,3),'123` = c(4, 5,6),check.names = FALSE)
select_(df,as.name(colName))


How can I write the argument of select 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 "__ID" 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 eval(parse()):

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).

eval(parse(text="`__ID`")

References about backtick characters in R:

解决方案

You can use as.name() with select_():

colName <- "__ID"
df <- data.frame(`__ID` = c(1,2,3), `123` = c(4,5,6), check.names = FALSE)
select_(df, as.name(colName))

这篇关于避免使用dplyr的反向字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆