Consider the following data.table:
x <- data.table(
          x=sample(letters[1:5],10,rep=T), 
          y=factor(sample(letters[1:5],10,rep=T), levels=letters))
This situation arises several times while working with data.tables where  some of the factor fields have unused variables. 
Now, if we use the following table:
table(x)
A giant table with all unused levels shows up.
Is there a way in table methods or data.table to do this?
I know that following is possible:
x$y <- factor(x$y)
But this is not useful because I don't want to save each of the sub-tables to a different variable.
You can use droplevel as follows
x[,y:=droplevels(y)]
this overwrites y by reference with droplevels(y)
Results in
> table(x)
   y
x   b c d e
  a 1 1 1 2
  b 0 1 0 0
  c 1 0 0 0
  d 1 0 0 0
  e 0 0 2 0
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With