data.table 中括号前的点,.( )

Dot preceding parentheses, .( ), in data.table

我不熟悉这种 df[, .(...), Col] 表示法。如果我遗漏了一些明显的东西,我深表歉意,但我找不到对这种符号风格的引用,尽管它看起来很有用。

它似乎正在实施聚合。根据下面代码中此符号的位置,我希望它来自 R 而不是 h2o,但我尝试检查两者都无济于事。

该示例来自 Kaggle 竞赛并且代码有效(重现它 go here):

trainHex<-as.h2o(train[,.(
  dist   = mean(radardist_km, na.rm = T),
  refArea5   = mean(Ref_5x5_50th, na.rm = T),
  refArea9  = mean(Ref_5x5_90th, na.rm = T),
  meanRefcomp = mean(RefComposite,na.rm=T),
  meanRefcomp5 = mean(RefComposite_5x5_50th,na.rm=T),
  meanRefcomp9 = mean(RefComposite_5x5_90th,na.rm=T),
  zdr   = mean(Zdr, na.rm = T),
  zdr5   = mean(Zdr_5x5_50th, na.rm = T),
  zdr9   = mean(Zdr_5x5_90th, na.rm = T),
  target = log1p(mean(Expected)),
  meanRef = mean(Ref,na.rm=T),
  sumRef = sum(Ref,na.rm=T),
  records = .N,
  naCounts = sum(is.na(Ref))
),Id][records>naCounts,],destination_frame="train.hex")

我希望文档and/or对此有很好的解释。

.() 是一个 data.table 便利函数,充当 list() 的简洁别名。让事情稍微复杂一点(主要是对于像你这样的人来说,试图弄清楚 . 到底做了什么!)是这样的事实,它只在调用 [=14= 的范围内被解释为这样的事实].

这里,来自?data.table

 DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
 setkey(DT,x,y)             # 2-column key

 DT["a"]                    # join to 1st column of key
 DT[.("a")]                 # same, .() is an alias for list()
 DT[list("a")]              # same

 ## But note that *this* doesn't work (my addition --- not in ?data.table)
 .("a")

另请参阅小插图 Introduction to data.table

data.table also allows wrapping columns with .() instead of list(). It is an alias to list(); they both mean the same. Feel free to use whichever you prefer