4

This is almost a duplicate of this. I want to drop columns from a data table, but I want to do it efficiently. I have a list of names of columns that I want to keep. All the answers to the linked question imply doing something akin to

data.table.new <- data.table.old[, my.list]

which at some crucial point will give me a new object, while the old object is still in memory. However, my data.table.old is huge, and hence I prefer to do this via reference, as suggested here

set(data.table.old, j = 'a', value = NULL)

However, as I have a whitelist of columns, and not a blacklist, I would need to iterate through all the column names, checks whether they are in my.list, and then apply set(). Is there any cleaner/other way to doing so?

2
  • 3
    use the setdiff of the variable names? Commented Jul 7, 2015 at 11:34
  • possible duplicate of Idiom for dropping a single column in a data.table mnel's answer there is easily extended to the drop-more-than-one-column case (with toDrop analogous to dropcols in Jan's answer). Commented Jul 7, 2015 at 16:55

1 Answer 1

8

Not sure if you can do by reference ops on data.frame without making it data.table.
Below code should works if you consider to use data.table.

library(data.table)
setDT(data.frame.old)
dropcols <- names(data.frame.old)[!names(data.frame.old) %in% my.list]
data.frame.old[, c(dropcols) := NULL]
Sign up to request clarification or add additional context in comments.

4 Comments

I think setdiff is better for construction of dropcols
@Frank Very likely, but I personally used to the not in vector subset
Why doesn't dropcols := NULL work? What is the c doing?
@Farrel it evaluates dropcols to the character vector. Your code wouldn’t work because it would try to remove column named "dropcols" instead of the column names which dropcols variables stores. It doesn't need to be c(dropcols), it can be (dropcols) which also evaluates the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.