Consider the following data.table:
data.table
x <- data.table( x=sample(letters[1:5],10,rep=T), y=factor(sample(letters[1:5],10,rep
You can use droplevel as follows
droplevel
x[,y:=droplevels(y)]
this overwrites y by reference with droplevels(y)
y
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