
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 0.41 KB | hits: 29 | expires: Never
How do you delete a column in data.table?
df=data.frame(id=1:100,foo=rnorm(100))
df2 <- df[-grep('foo',colnames(df))] # works
df3=data.table(df)
df3[-grep('foo',colnames(df3))]
# Method 1
df3[,foo:=NULL]
# Method 2
df3 <- df3[,-grep("foo", colnames(df3)), with=FALSE]
# Method 3 -- Safer than Method 2 -- see Joshua Ulrich's comments above and below
df3 <- df3[, which(!grepl("foo", colnames(df3))), with=FALSE]