Completely deleting data rows in R. Stop table () returns 0 for deleted data

I want to create a piechart from the number of occurrences in the dataframe column. However, there are 5 words that I want to delete before I make a piechart. I have a dataframe and I can remove these lines using something like:

subset(df, tag != "rubbish") 

However, if I then draw using

 pie(table(df$tag) 

Trash still appears in piechart, but with Zero instances.

table(df$tag) before the subset gives me something like:

 tag1 3 tag2 7 rubbish 9 

and after:

 tag1 3 tag2 7 rubbish 0 

Do I need to delete garbage at all without a table () returning zero? I tried table(df$tag, exclude="rubbish") , but there is more than one tag that I want to remove.

+4
source share
1 answer

I think the problem is that df$tag is a factor or categorical variable. Removing a specific category from a coefficient does not change the levels associated with the factor. table iterates over the levels associated with the factor. Even if the level is empty, table does this, hence rubbish 0 . The solution is to recreate the factor after removing the tag:

 subset(df, tag != "rubbish") levels(df$tag) df$tag = factor(df$tag) levels(df$tag) table(df$tag) 

I think this should solve your problem.

+5
source

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


All Articles