R: how to delete columns in a data table?

Question about the R.table data table: how do multiple data.table columns get deleted in a memory efficient way?

Suppose the column names to be deleted are stored in the deleteCol vector.

In a data.frame, it is:
DF <- DF[deleteCol] <- list()

For data.table, I tried:

DT[, deleteCol, with=FALSE] <- list()

but it gave unused argument(s) (with = FALSE)

Thank!

+5
source share
1 answer

ok here are a few options. The latter seems to be exactly what you want ...

 x<-1:5
 y<-1:5
 z<-1:5
 xy<-data.table(x,y,z)
 NEWxy<-subset(xy, select = -c(x,y) ) #removes column x and y

and

id<-c("x","y")
newxy<-xy[, id, with=FALSE]
newxy #gives just x and y e.g.

   #  x y
#[1,] 1 1
#[2,] 2 2
#[3,] 3 3
#[4,] 4 4
#[5,] 5 5

and finally what you really want:

anotherxy<-xy[,id:=NULL,with=FALSE] # removes comuns x and y that are in id

#     z
#[1,] 1
#[2,] 2
#[3,] 3
#[4,] 4
#[5,] 5
+5
source

All Articles