data.table lapply(.SD)的问题

时间:2017-06-28 17:49:26

标签: r data.table

data.table过去曾在.SDcols lapply(.SD, foo)内有力地允许jdt[i,j,...]的组合。现在似乎没有。是否更改了正确的语法?是否不再支持该操作?任何帮助将不胜感激!

以下示例:

library(data.table)
set.seed(1234L)
dt <- data.table(matrix(rnorm(1e5), ncol= 20))

# This used to work fine , now it returns 0 rows
dt <- dt[, lapply(.SD, floor), .SDcols= c(3:6, 15:20), with=FALSE]
# Once that stopped, this worked as substitute
# now it gives the below error
dt[,c(3:6, 15:20), with=FALSE] <- dt[,c(3:6, 15:20), with=FALSE][,lapply(.SD, floor)]
  

[<-.data.table*tmp*,c(3:6,15:20)中的错误,= FALSE,值= list(:     unused参数(使用= FALSE)

R> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.4
# also fails on: Running under: CentOS Linux 7 (Core)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

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

loaded via a namespace (and not attached):
[1] tools_3.3.2

Cross github/data.table

报道

1 个答案:

答案 0 :(得分:1)

基于@ michaelchirico的评论,似乎.SDcols必须在data.table的最新版本中指定为字符,这回答了我的问题:

out_cols <- in_cols <- names(dt)[c(3:6, 15:20)]
dt[, c(out_cols) := lapply(.SD, floor), .SDcols= in_cols]
head(dt)
           V1         V2 V3 V4 V5 V6        V7         V8           V9         V10         V11
1: -1.2070657 -0.8251442 -2  1 -2 -2 2.4918186  2.0606137 -0.903147902 -0.77605164  0.49060054
2:  0.2774292  0.3471682  0 -1 -1 -1 0.0532215  0.9621719 -0.006098308  0.32369237  0.02499143
3:  1.0844412 -0.9200929  0  0 -1  0 0.4562491 -0.5479694 -0.904131937  0.53358555  1.29905349
4: -2.3456977 -0.2873365  0  0  0 -1 1.5770552 -0.5066129 -0.060453158  0.33302666 -0.23457321
5:  0.4291247 -0.5511303  1  0 -2  0 0.6223530  0.6331848 -1.094187464  0.72907524 -0.45257621
6:  0.5060559  0.8486456 -1 -3 -2  1 1.1879753  1.0108679  0.352918538 -0.07796231 -0.01112573
           V12        V13         V14 V15 V16 V17 V18 V19 V20
1: -0.21065753 -1.4312554  0.32158599  -1  -1   0   0  -1  -1
2: -0.06161771  0.1261129  0.49759863  -1  -1  -1   1   0   0
3:  0.40307363  0.4368590  1.51966273  -1   0  -1  -1  -1   0
4:  0.91924949  0.1487486  0.62423587  -2  -1   1  -2  -1  -1
5:  0.36805999 -1.0242141  1.11671721   0  -3   0   0  -1   0
6: -1.21098232  0.2602200 -0.04977916   0   0  -2   1   0   0