可以从.data.table()中的浏览器查看.SD吗? [英] Can .SD be viewed from a browser within [.data.table()?
问题描述
在构建表达式以放入 [。data.table
调用的 j
通常有助于检查和玩弄 .SD
的内容。
While constructing expressions to put in the j
-slot of a [.data.table
call, it would often be helpful to be able to examine and play around with the contents of .SD
.
这种天真的尝试't work ...
This naive attempt doesn't work...
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT[, browser(), by=x]
# Called from: `[.data.table`(DT, , browser(), by = x)
Browse[1]>
Browse[1]> .SD
# NULL data.table
...即使一个名为 .SD
以及与当前data.table子集相关的其他几个都存在于本地环境中
... even though a variable named .SD
and several others related to the current data.table subset are all present in the local environment
Browse[1]> ls(all.names = TRUE)
# [1] ".BY" ".GRP" ".I" ".iSD" ".N" ".SD"
# [7] "Cfastmean" "mean" "print" "x"
Browse[1]> .N
# [1] 3
Browse[1]> .I
# [1] 4 5 6
使用。 I
,我可以查看一些+/-像 .SD
,但它是很好能够直接访问它的值:
Using .I
, I can view something +/- like .SD
, but it would be nice to be able to directly access its value:
Browse[1]> DT[.I]
# x y v
# 1: b 1 4
# 2: b 3 5
# 3: b 6 6
我的问题:为什么 .SD
不能直接从浏览器()
调用( .I
, .N
, .GRP
和 .BY
are)?是否有其他方法可以访问 .SD
?
My questions: Why is the expected value of .SD
not directly available from within a browser()
call (while .I
, .N
, .GRP
and .BY
are)? Is there some alternative way to access the value of .SD
?
推荐答案
> 根据Matthew Dowle的评论更新:
Updated in light of Matthew Dowle's comments:
结果是 .SD
是内部评估所有 j
表达式的环境,包括未明确引用 .SD的环境
。为 DT
的每个子集填充所有 DT
的列不便宜,因此 [。data.table()
不会这样做,除非它真的需要。
It turns out that .SD
is, internally, the environment within which all j
expressions are evaluated, including those which don't explicitly reference .SD
at all. Filling it with all of DT
's columns for each subset of DT
is not cheap, timewise, so [.data.table()
won't do so unless it really needs to.
相反,充分利用R对参数的延迟评估,它预览了未评估的 j
表达式,添加到其中引用的 .SD
列。如果提及 .SD
本身,则会添加 DT
的所有列。
Instead, making great use of R's lazy-evaluation of arguments, it previews the unevaluated j
expression, and only adds to .SD
columns that are referenced therein. If .SD
itself is mentioned, it adds all of DT
's columns.
因此,要查看 .SD
,只需在 j
表达。这里是许多表达式中的一个将工作:
So, to view .SD
, just include some reference to it in the j
-expression. Here is one of many expressions that will work:
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
## This works
DT[, if(nrow(.SD)) browser(), by=x]
# Called from: `[.data.table`(DT, , if (nrow(.SD)) browser(), by = x)
Browse[1]> .SD
# y v
# 1: 1 1
# 2: 3 2
# 3: 6 3
这里有几个:
DT[,{.SD; browser()}, by=x]
DT[,{browser(); .SD}, by=x] ## Notice that order doesn't matter
.SD
只是加载 j
表达式所需的列,依次运行这些列(键入 .SD
进入浏览器环境, Q
离开并返回正常的命令行):
To see for yourself that .SD
just loads columns needed by the j
-expression, run these each in turn (typing .SD
when entering the browser environment, and Q
to leave it and return to the normal command-line):
DT[, {.N * y ; browser()}, by=x]
DT[, {v^2 ; browser()}, by=x]
DT[, {y*v ; browser()}, by=x]
这篇关于可以从.data.table()中的浏览器查看.SD吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!