How to select only numeric columns from a data table
Another solution without with = FALSE
is the .SDcols
parameter:
x[, .SD, .SDcols = which(sapply(x, is.numeric))]
This also works:
x[, .SD, .SDcols = sapply(x, is.numeric)]
The current development version (as of 2020-04-16
) also makes this one step simpler:
x[ , .SD, .SDcols = is.numeric]
data.table
needs the with=FALSE
to grab column numbers.
tokeep <- which(sapply(x,is.numeric))
x[ , tokeep, with=FALSE]