Filter rows in R based on values in multiple rows

Viewed 4251

I'm trying to filter out multiple rows of data that I don't need in R but I'm not sure how to do it.

The data I'm using looks a bit like this:

  Category     Item Shop1 Shop2 Shop3
1    Fruit   Apples     4     6     0
2    Fruit  Oranges     0     2     7
3      Veg Potatoes     0     0     0
4      Veg   Onions     0     0     0
5      Veg  Carrots     0     0     0
6    Dairy  Yoghurt     0     0     0
7    Dairy     Milk     0     1     0
8    Dairy   Cheese     0     0     0

I only want to keep categories where at least one item has a positive value for at least one of the shops.

In this case, I want to get rid of all the Veg rows, because none of the shops have sold any vegetables. I want to keep all the Fruit rows, and I want to keep all the Dairy rows, even those with a value of zero across all shops, because one of the Dairy rows does have a value above 0.

I tried to use colSums after using group_by(Category) in the hope that it would just sum the contents of the Category each time, but it didn't work. I've also tried to add a column at the end for rowSums and to filter based on frequency, but I could only filter out individual rows this way, not rows based on the whole Category.

While I can filter out individual rows that have values of zero (like row 3 for example), my difficulty is keeping in rows like rows 6 and 8 where all the values for each shop are zero, but I want to keep these rows because other Dairy rows do have values above zero.

2 Answers
Related