data.table 中的非等值连接 - 反引号列名问题
non-equi-joins in R with data.table - backticked column name trouble
当(反引号)列名包含 space.
时,我无法使用 data.table 进行非等值连接
我在工作中从我们的数据库中收集了这些名字,我们的明确政策是每个人都使用这些相同的名字以避免混淆。我当然可以转换和重新转换,但我宁愿避免这种情况。
我想知道,这是 data.table 中的一个故障吗?如果是,是否可以修复?或者我错过了什么?我对 R 很陌生,所以后者是完全可能的...
一个可重现的例子:
以下内容有效:
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)]
错误信息是:
Error in [.data.table
(a, b, on = .(test name1
= test name4
, test name2
> :
Column(s) [test name2
,test name2
] not found in x
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=
是另一种选择:
a[b, on = c("test name1==test name4", "test name2>test name3", "test name2<=V2")]
我认为这仅在 equality/inequality 运算符周围没有空格并且使用 ==
而不是 =
时才有效。
我不确定是否有办法按照 OP 的代码编写 on=
,尽管看起来应该有。
当(反引号)列名包含 space.
时,我无法使用 data.table 进行非等值连接我在工作中从我们的数据库中收集了这些名字,我们的明确政策是每个人都使用这些相同的名字以避免混淆。我当然可以转换和重新转换,但我宁愿避免这种情况。
我想知道,这是 data.table 中的一个故障吗?如果是,是否可以修复?或者我错过了什么?我对 R 很陌生,所以后者是完全可能的...
一个可重现的例子:
以下内容有效:
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)]
错误信息是:
Error in
[.data.table
(a, b, on = .(test name1
=test name4
,test name2
> : Column(s) [test name2
,test name2
] not found in x
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=
是另一种选择:
a[b, on = c("test name1==test name4", "test name2>test name3", "test name2<=V2")]
我认为这仅在 equality/inequality 运算符周围没有空格并且使用 ==
而不是 =
时才有效。
我不确定是否有办法按照 OP 的代码编写 on=
,尽管看起来应该有。