Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error deleting factor column in empty data.table

Tags:

r

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.

like image 596
Frank Avatar asked Aug 06 '13 20:08

Frank


1 Answers

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.

like image 100
Matt Dowle Avatar answered Sep 30 '22 19:09

Matt Dowle