To get rid of a column named “foo” in a data.frame, I can do:
df <- df[-grep('foo', colnames(df))]
However, once df is converted to a data.table object, there is no way to just remove a column.
Example:
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))]
But once it is converted to a data.table object, this no longer works.
Any of the following will remove column
foofrom the data.tabledf3:data.table also supports the following syntax:
though if you were actually wanting to remove column
"foo"fromdf3(as opposed to just printing a view ofdf3minus column"foo") you’d really want to use Method 1 instead.(Do note that if you use a method relying on
grep()orgrepl(), you need to setpattern="^foo$"rather than"foo", if you don’t want columns with names like"fool"and"buffoon"(i.e. those containingfooas a substring) to also be matched and removed.)Less safe options, fine for interactive use:
The next two idioms will also work — if
df3contains a column matching"foo"— but will fail in a probably-unexpected way if it does not. If, for instance, you use any of them to search for the non-existent column"bar", you’ll end up with a zero-row data.table.As a consequence, they are really best suited for interactive use where one might, e.g., want to display a data.table minus any columns with names containing the substring
"foo". For programming purposes (or if you are wanting to actually remove the column(s) fromdf3rather than from a copy of it), Methods 1, 2a, and 2b are really the best options.Lastly there are approaches using
with=FALSE, thoughdata.tableis gradually moving away from using this argument so it’s now discouraged where you can avoid it; showing here so you know the option exists in case you really do need it: