Reputation: 4907
data.table
used to powerfully allow combinations of .SDcols
and lapply(.SD, foo)
within the j
of dt[i,j,...]
. Now it doesn't seem to. Has the proper syntax changed? Is the operation no longer supported? Any help would be greatly appreciated!
example below:
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)]
Error in
[<-.data.table
(*tmp*
, , c(3:6, 15:20), with = FALSE, value = list( : unused argument (with = 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 reported on github/data.table
Upvotes: 0
Views: 1702
Reputation: 4907
Based on @michaelchirico's comment, it would appear that .SDcols
must be specified as characters in the latest version of data.table
, which answers my question:
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
Upvotes: 1