Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:03:34+00:00 2026-05-29T10:03:34+00:00

To get rid of a column named foo in a data.frame , I can

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:03:35+00:00Added an answer on May 29, 2026 at 10:03 am

    Any of the following will remove column foo from the data.table df3:

    # 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]
    

    data.table also supports the following syntax:

    ## Method 3 (could then assign to df3, 
    df3[, !"foo"]  
    

    though if you were actually wanting to remove column "foo" from df3 (as opposed to just printing a view of df3 minus column "foo") you’d really want to use Method 1 instead.

    (Do note that if you use a method relying on grep() or grepl(), you need to set pattern="^foo$" rather than "foo", if you don’t want columns with names like "fool" and "buffoon" (i.e. those containing foo as a substring) to also be matched and removed.)

    Less safe options, fine for interactive use:

    The next two idioms will also work — if df3 contains 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) from df3 rather than from a copy of it), Methods 1, 2a, and 2b are really the best options.

    # Method 4:
    df3[, .SD, .SDcols = !patterns("^foo$")]
    

    Lastly there are approaches using with=FALSE, though data.table is 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:

    # Method 5a (like Method 3)
    df3[, !"foo", with=FALSE] 
    # Method 5b (like Method 4)
    df3[, !grep("^foo$", names(df3)), with=FALSE]
    # Method 5b (another like Method 4)
    df3[, !grepl("^foo$", names(df3)), with=FALSE]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get rid of: <input type=hidden name=__VIEWSTATE id=__VIEWSTATE value=.../> Completely !
Does anyone know how I can get rid of the following assembler warning? Code
I have a error that I can not get rid of, but the code
How can I get rid of the empty space by components not rendered via
I have a table locales with a column named name. The records in name
I'm trying to get rid of everything before the filename ex: /home/mike/whatever/test_1.txt leaving only
Is it possible to get rid of the eval statement below? The code below
I'm trying to get rid of floats in my functions. I have one function
How do I get rid of the deprecated conversion warning in CDT? I know
I'm trying to get rid of curly apostrophes (ones pasted from some sort of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.