How to drop unused levels in table with data.table?

前端 未结 1 1108
盖世英雄少女心
盖世英雄少女心 2021-01-18 15:58

Consider the following data.table:

x <- data.table(
          x=sample(letters[1:5],10,rep=T), 
          y=factor(sample(letters[1:5],10,rep         


        
相关标签:
1条回答
  • 2021-01-18 16:37

    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
    
    0 讨论(0)
提交回复
热议问题