How do you delete a column by name in data.table?

前端 未结 8 1048
北海茫月
北海茫月 2020-12-02 03:52

To get rid of a column named \"foo\" in a data.frame, I can do:

df <- df[-grep(\'foo\', colnames(df))]

However, once df

8条回答
  •  离开以前
    2020-12-02 04:42

    I simply do it in the data frame kind of way:

    DT$col = NULL
    

    Works fast and as far as I could see doesn't cause any problems.

    UPDATE: not the best method if your DT is very large, as using the $<- operator will lead to object copying. So better use:

    DT[, col:=NULL]
    

提交回复
热议问题