Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Any of the following will remove column <code>foo</code> from the data.table <code>df3</code>:</p> <pre><code># Method 1 (and preferred as it takes 0.00s even on a 20GB data.table) df3[,foo:=NULL] df3[, c("foo","bar"):=NULL] # remove two columns myVar = "foo" df3[, (myVar):=NULL] # lookup myVar contents # Method 2a -- A safe idiom for excluding (possibly multiple) # columns matching a regex df3[, grep("^foo$", colnames(df3)):=NULL] # Method 2b -- An alternative to 2a, also "safe" in the sense described below df3[, which(grepl("^foo$", colnames(df3))):=NULL] </code></pre> <p><strong>data.table</strong> also supports the following syntax:</p> <pre><code>## Method 3 (could then assign to df3, df3[, !"foo", with=FALSE] </code></pre> <p>though if you were actually wanting to remove column <code>"foo"</code> from <code>df3</code> (as opposed to just printing a view of <code>df3</code> minus column <code>"foo"</code>) you'd really want to use Method 1 instead.</p> <p>(Do note that if you use a method relying on <code>grep()</code> or <code>grepl()</code>, you need to set <code>pattern="^foo$"</code> rather than <code>"foo"</code>, if you don't want columns with names like <code>"fool"</code> and <code>"buffoon"</code> (i.e. those containing <code>foo</code> as a substring) to also be matched and removed.)</p> <h3>Less safe options, fine for interactive use:</h3> <p>The next two idioms will also work -- <strong>if <code>df3</code> contains a column matching <code>"foo"</code></strong> -- 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 <code>"bar"</code>, you'll end up with a zero-row data.table.</p> <p>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 <code>"foo"</code>. For programming purposes (or if you are wanting to actually remove the column(s) from <code>df3</code> rather than from a copy of it), Methods 1, 2a, and 2b are really the best options.</p> <pre><code># Method 4a: df3[, -grep("^foo$", colnames(df3)), with=FALSE] # Method 4b: df3[, !grepl("^foo$", colnames(df3)), with=FALSE] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload