Ifad Noor
Ifad Noor

Reputation: 125

Filter a chunk of rows based on a specific value in another column in R

Example of Constricted Data Table:

img

I would like to be able to filter out rows of data based on if a particular value exists in another column. The rows I would like to filter out would all have the same "Material" #. In the example I provided, the Material #U83231036 has the value, "ZHLB (ConAgra Semifinished prod)" in one of the two rows in the "Material_Type_Comp" column. I want to be able to extract out the two rows of data related to that Material # because that value exists in the "Material_Type_Comp" column for one of the rows.

What is the best way to go about doing this?

Upvotes: 1

Views: 194

Answers (1)

akrun
akrun

Reputation: 886968

One option is to do a filter by group

library(dplyr)
df1 %>%
   group_by(Material) %>%
   filter("ZHLB (ConAgra Semifinished prod)" %in% Material_Type_Comp)
   #or use any with `==`
   #filter(any(Material_Type_Comp == "ZHLB (ConAgra Semifinished prod)")

Upvotes: 1

Related Questions