简体   繁体   English

在列名中加入大于和小于号的 data.tables 时出现问题

[英]Issue when joining data.tables with greater than and lesser than signs in column names

When column names have > (greater than) or < (lesser than) sign and I try to join on these columns, the column with the sign gets trimmed and I get an error like this one:当列名称具有 >(大于)或 <(小于)符号并且我尝试加入这些列时,带有符号的列被修剪,并且出现如下错误:

Error in colnamesInt(x, names(on), check_dups = FALSE) : argument specifying columns specify non existing column(s): cols[1]='COLUMN WITH' colnamesInt(x, names(on), check_dups = FALSE) 中的错误:指定列的参数指定不存在的列:cols[1]='COLUMN WITH'

Code to reproduce the issue重现问题的代码

library(data.table)

table_a <- data.table(
  `COLUMN WITH >FAILS` = character(),
  BLA = integer(),
  BLO = integer()
  
)

table_b <- data.table(
  `COLUMN WITH >FAILS` = character(),
  BLA = integer(),
  BLI = integer(),
  BLU = integer()
  
)

on = setdiff(names(table_a), c("BLA", "BLU", "BLO"))

table_a[table_b, on = on]

My scrappy workaround was to replace the special character before the join and put the original column name after the join, it works but doesn't look very elegant.我草率的解决方法是在连接之前替换特殊字符并将原始列名称放在连接之后,它可以工作但看起来不太优雅。 I don't know if that is a known issue, I haven't found any documentation about this.我不知道这是否是一个已知问题,我还没有找到任何关于此的文档。 Can you think of a better way of solving this?你能想出更好的方法来解决这个问题吗?

Backticks inside quotes allow to use reserved symbols in the join :引号内的反引号允许在连接中使用保留符号:

table_a[table_b, on = "`COLUMN WITH >FAILS`"]

Empty data.table (0 rows and 6 cols): COLUMN WITH >FAILS,BLA,BLO,i.BLA,BLI,BLU

Or more generally:或更一般地说:

table_a[table_b, on = paste0("`",on,"`")]

Empty data.table (0 rows and 6 cols): COLUMN WITH >FAILS,BLA,BLO,i.BLA,BLI,BLU

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM