How to exclude one column from data.table OR convert to data.table to MTS

Solution 1:

Try with=FALSE :

dt[,-1,with=FALSE]

As an aside, feature request #416 is related :

Add not join DT[-J(...)], and not columns DT[,-"colC",with=FALSE].

Solution 2:

The previous answer works, but here is the answer with the updated methods:

## CONVERT TO ZOO

#### old method
zooObj = zoo(x=dt[,-1,with=FALSE], order.by=dt$mydates)

#### alternatives
zooObj = zoo(x=dt[,!'mydates'], order.by=dt$mydates)
zooObj = zoo(x=dt[,-'mydates'], order.by=dt$mydates)

#### and if column name is a variable
myvar = 'mydates'
zooObj = zoo(x=dt[,!..myvar], order.by=dt[[myvar]])
zooObj = zoo(x=dt[,-..myvar], order.by=dt[[myvar]])

As an extension, if you wanted to remove more than one column from the data.table, try one of the following:

dt[,-c('value1', ..myvar)]
dt[, .SD, .SDcols = !c('value1', (myvar))]