在data.table中使用.EACHI时如何访问`i`的所有列 [英] How can I access all columns of `i` when using .EACHI in data.table
问题描述
根据?data.table
使用 x [i,j]
时,其中 i
是 data.table
, i
的列可通过<$ c访问 j
中的$ c> i.column 。
我的问题是如何一次访问所有列?
as per ?data.table
when using x[i,j]
where i
is a data.table
, columns of i
can be accessed with i.column
in j
.
My question is how can I access ALL columns at once ?
示例:
library(data.table)
set.seed(1L)
DT <- data.table(A=c('a','a','b','b','c','c'),
B=rnorm(6L),
C=rnorm(6L),
key='A')
# A B C
#1: a 0.82122120 0.61982575
#2: a 0.59390132 -0.05612874
#3: b 0.91897737 -0.15579551
#4: b 0.78213630 -1.47075238
#5: c 0.07456498 -0.47815006
#6: c -1.98935170 0.41794156
DT2 <- data.table(A=c('a','b','c'),
B=rnorm(3L),
C=rnorm(3L),
key='A')
# A B C
#1: a 1.3586796 -0.05380504
#2: b -0.1027877 -1.37705956
#3: c 0.3876716 -0.41499456
现在说我想将 DT
的列 B
乘以数字 B
DT2
中的code> A
列的每个匹配值。
也就是说
Now say I want to multiply the column B
of DT
by the number B
of DT2
for each matching value of column A
.
That is to say
0.82122120 * 1.3586796
0.59390132 * 1.3586796
...
我可以执行以下操作,但这仅是因为我知道这些列称为 B
和 C
,因为我有足够的耐心写所有列。
I can do the following, but only because I know the columns are called B
and C
, and because I an patient enough to write all columns.
我如何用100列做类似的事情?
How could I do a similar thing with 100 columns for instance ?
DT[DT2,{print(as.matrix(.SD) %*% diag(c(i.B,i.C)))},by=.EACHI]
[,1] [,2]
[1,] 1.1157764 -0.033349750
[2,] 0.8069216 0.003020009
[,1] [,2]
[1,] -0.09445960 0.2145397
[2,] -0.08039401 2.0253136
[,1] [,2]
[1,] 0.02890673 0.1984297
[2,] -0.77121518 -0.1734435
推荐答案
实际上,似乎没有特殊符号,如 x.SD
或 i.SD
指定所有列(在 by = ...
中使用的列除外。
Indeed, there seem to be no special symbols like x.SD
or i.SD
to specify all columns (except the ones used in by = ...
.
但是, mget()
可以用作返回预期结果的解决方法:
However, mget()
can be used as a workaround which returns the expected results:
DT2_cols <- paste0("i.", setdiff(names(DT2), key(DT2)))
DT[DT2,{print(as.matrix(.SD) %*% diag(mget(DT2_cols)))},by=.EACHI]
[,1] [,2]
[1,] 0.3891785 -0.02190195
[2,] -0.1140867 -0.03317559
[,1] [,2]
[1,] 1.850667 -0.009322052
[2,] -3.533068 0.004944318
[,1] [,2]
[1,] 0.3706735 1.4268738
[2,] -0.9229703 0.3679482
Empty data.table (0 rows) of 3 cols: A,B,C
(请注意,数字与OP不同,因为使用 set.seed(1L)
我确实得到了不同的 DT
和 DT2
,而不是由OP发布。)
(Note that the numbers differ from OP's because using set.seed(1L)
I do get different DT
and DT2
than posted by the OP.)
我刚刚注意到已经在GitHub >上已请求此功能。
I just noticed that this feature already has been requested on GitHub.
这篇关于在data.table中使用.EACHI时如何访问`i`的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!