Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 0.41 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do you delete a column in data.table?
  2. df=data.frame(id=1:100,foo=rnorm(100))
  3. df2 <- df[-grep('foo',colnames(df))] # works
  4. df3=data.table(df)
  5. df3[-grep('foo',colnames(df3))]
  6.        
  7. # Method 1
  8. df3[,foo:=NULL]
  9.  
  10. # Method 2
  11. df3 <- df3[,-grep("foo", colnames(df3)), with=FALSE]
  12.  
  13. # Method 3 -- Safer than Method 2 -- see Joshua Ulrich's comments above and below
  14. df3 <- df3[, which(!grepl("foo", colnames(df3))), with=FALSE]