[英]Filter a chunk of rows based on a specific value in another column in R
Example of Constricted Data Table:受限数据表示例:
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.在我提供的示例中,材料 #U83231036 在“Material_Type_Comp”列的两行之一中具有值“ZHLB (ConAgra Semifinished prod)”。 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.我希望能够提取与该材料# 相关的两行数据,因为该值存在于其中一行的“Material_Type_Comp”列中。
What is the best way to go about doing this? go 关于这样做的最佳方法是什么?
One option is to do a filter
by group一种选择是按组进行filter
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)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.