2012-05-14 1 views
5

Domanda relativa al pacchetto R data.table: come vengono rimosse più colonne data.table in un modo di memoria efficiente ?R: come eliminare le colonne in un data.table?

Supponiamo che i nomi delle colonne da eliminare siano memorizzati nel vettore deleteCol.

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

Per data.table, ho provato:

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

ma questo ha dato unused argument(s) (with = FALSE)

Grazie!

risposta

5

ok qui ci sono alcune opzioni. L'ultimo sembra esattamente quello che vuoi ...

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 

e

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 

e, infine, ciò che si vuole veramente:

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 
+0

Che fa il trucco! Grazie! –

+4

+1. Non c'è bisogno però di 'anotherxy <-'. ': =' modifica 'xy' per riferimento. Se vuoi una copia modificata allora devi 'copy()' esplicitamente; ad esempio, 'anotherxy <- copy (xy) [, id: = NULL, con = FALSE]'. –