Table R function - how to remove 0 counters?

I need to remove rows from the output of a table function that have 0 counters in all columns. Is there an easy way to do this?

table(ds$animal,ds$gender) ___ | M | F Cat | 9 | 4 Dog | 0 | 0 Rat | 4 | 3 

I just would like to see these lines:

 ___ | M | F Cat | 9 | 4 Rat | 4 | 3 
+6
source share
1 answer

you need to reset levels from the animal factor.

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

you can also just delete them from ds and then make a table

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

I used here because it prints the headers.

+8
source

Source: https://habr.com/ru/post/985152/


All Articles