Frank
Frank

Reputation: 2416

Error deleting factor column in empty data.table

If I have an empty data.table with a factor column, the factor column can't be removed with the := NULL operator. Integer and character columns have no problems.

library(data.table)
DT <- data.table(numbers = integer(0),
                char.letters = character(0),
                factor.letters = factor(character(0)))
DT[, factor.letters := NULL]

I get the following error:

Error in `[.data.table`(DT, , `:=`(factor.letters, NULL)) : 
Can't assign to column 'factor.letters' (type 'factor') a value of type 'NULL' (not character, factor, integer or numeric)

Note that DT[, char.letters := NULL] and DT[, numbers := NULL] do not produce errors.

Since factor columns behave differently from character and integer columns, I suspect this is a problem with data.table, but am I doing anything incorrectly?

Edit: Previous example used join to create the empty data.table (which was then called join), but it can be reproduced just as easily by creating it directly.

Upvotes: 13

Views: 573

Answers (1)

Matt Dowle
Matt Dowle

Reputation: 59612

Thanks for reporting. Now fixed in v1.8.9

Deleting a (0-length) factor column using :=NULL on an empty data.table now works, #4809. Thanks to Frank Pinter for reporting. Test added.

Upvotes: 3

Related Questions