Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R table function - how to remove 0 counts?

Tags:

r

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)

___ | M | F

Cat | 9 | 4 

Dog | 0 | 0

Rat | 4 | 3

I just would like to see those rows:

___ | M | F

Cat | 9 | 4 

Rat | 4 | 3
like image 761
michalrudko Avatar asked Apr 15 '15 00:04

michalrudko


1 Answers

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$animal <- droplevels(ds$animal)
with(ds, table(animal,gender))

here I used with because it prints headers.

like image 57
pdb Avatar answered Sep 19 '22 08:09

pdb