I want to find two strings in multiple columns for a particular item. I need to get the colnames which contain "0/1" or "1/1" for each item in Item_Name.
Example dataframe:
| Item_Name | Sample_A | Sample_B | Sample_C |
|---|---|---|---|
| Nino1 | 0/1 | 0/0 | 1/1 |
| Nino2 | 0/0 | 1/1 | 0/1 |
| Nino3 | 1/1 | 0/1 | 0/0 |
Expected Output:
Nino1 - SampleA, Sample_C
Nino2 - Sample_B, Sample_C
Nino3 - Sample_A, Sample_B
Till now I know to use grepl to find one pattern at time. I am not sure how to get colnames part.
data_frame_mod <- dataframe[Reduce(`|`, lapply(dataframe, grepl, pattern = "0/1"))]
Thanks
Here's a fairly rudimentary way of doing it:
Update: a one-liner:
Output: