R中带有data.table的非等值联接-反引号列名称问题 [英] non-equi-joins in R with data.table - backticked column name trouble

查看:173
本文介绍了R中带有data.table的非等值联接-反引号列名称问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当(反引号)列名称包含空格时,我无法与data.table进行非等联接。

I can't manage to do a non-equi-join with data.table when (backticked) column names include a space.

我从我们的数据库在工作,我们明确的政策是让每个人都使用相同的名称以避免混淆。我当然可以进行转换和重新转换,但是我希望避免这种情况。

I collect such names from our database at work, and our explicit policy is for everyone to use those same names to avoid confusion. I could of course convert and reconvert, but I'd prefer to avoid that.

我想知道这是否是data.table中的一个小故障,如果可以的话,可以吗?补救?还是我错过了什么?我对R很陌生,所以完全有可能...

I wonder, is this a glitch in data.table, and if so, can it be remedied? Or am I missing something? I'm quite new to R, so the latter is entirely possible...

一个可重现的示例:

以下的方法可以工作:

a <- data.table(`test name1` = c('A', 'A', 'A', 'B', 'B'),
                 `test_name2` = c(1,2,3,3,4))

b <- data.table(`test_name3` = c(0,1,2),
                 `test name4` = c('A', 'A', 'B'),
                 V2 = c(1,2,3),
                 V3 = c('Low', 'Medium', 'High'))

a[b, on = .(`test name1` = `test name4`, `test_name2` > `test_name3`, `test_name2` <= V2)]

以下内容不是:

a <- data.table(`test name1` = c('A', 'A', 'A', 'B', 'B'),
                 `test name2` = c(1,2,3,3,4))

b <- data.table(`test name3` = c(0,1,2),
                 `test name4` = c('A', 'A', 'B'),
                 V2 = c(1,2,3),
                 V3 = c('Low', 'Medium', 'High'))

a[b, on = .(`test name1` = `test name4`, `test name2` > `test name3`, `test name2` <= V2)]

错误消息是:


[。data.table (a,b,on =。( 测试名称1 = 测试名称4 测试名称2 >:
在x

Error in [.data.table(a, b, on = .(test name1 = test name4, test name2 > : Column(s) [test name2,test name2] not found in x

sessionInfo():

sessionInfo():

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=Norwegian (Bokmål)_Norway.1252  LC_CTYPE=Norwegian (Bokmål)_Norway.1252    LC_MONETARY=Norwegian (Bokmål)_Norway.1252
[4] LC_NUMERIC=C                               LC_TIME=Norwegian (Bokmål)_Norway.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.11.4


推荐答案

用字符串指定 on = 是另一种选择:

Specifying on= with strings is another option:

a[b, on = c("test name1==test name4", "test name2>test name3", "test name2<=V2")]

我认为只有在没有在等号/不等号运算符和 == 周围使用空格,而不是 =

I think this works only if there is no whitespace around the equality/inequality operators and == is used instead of =.

我不确定是否可以按照OP的代码编写 on = 的方法,尽管似乎应该这样做。

I'm not sure if there's a way to write the on= along the lines of the OP's code, though it seems like there should be.

这篇关于R中带有data.table的非等值联接-反引号列名称问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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