R table function - how to remove 0 counts?

后端 未结 1 1337
小蘑菇
小蘑菇 2021-01-11 11:54

I need to remove the rows from the table function output, which have 0 counts in all the columns. Is there any easy way to do that?

table(ds$animal,ds$gender         


        
相关标签:
1条回答
  • 2021-01-11 12:17

    you need to drop levels from the factor animal.

    table(droplevels(ds$animal),ds$gender)
    

    you can also just drop them from ds and then do the table

    ds$anima <- droplevels(ds$animal)
    with(ds, table(animal,gender))
    

    here I used with because it prints headers.

    0 讨论(0)
提交回复
热议问题