Filter values in a column that have only one unique adjacent value

I really need your help on this, cant figure a way to do it.

I have added a picture of an example that illustrates the challenge I am having. I am trying to solve this in R. In the left hand table I have a column of items (hammer, saw, ax) and they have adjacent column of vendor number.

I am trying to filter out all items that have only one unique vendor number, basically single sourced items. Table on the right hand side in the picture represents the outcome I am looking for. It has identified items that have only one vendor and filtered all rows of that item.

If you have any idea I would greatly appreciate it. Huge thanks in advance.

image

There is probably a more elegant way to do this, but this gets to the goal.

DF <- data.frame(Item = c("hammer", "hammer", "hammer", "saw", "saw", "ax", "ax", "ax"),
                 Vendor = c(1,2,1,3,3,5,4,4))
DF
#>     Item Vendor
#> 1 hammer      1
#> 2 hammer      2
#> 3 hammer      1
#> 4    saw      3
#> 5    saw      3
#> 6     ax      5
#> 7     ax      4
#> 8     ax      4
library(dplyr)

SingleVendor <- DF %>% group_by(Item) %>% summarize(N = length(unique(Vendor))) %>% filter(N == 1)
SingleVendor
#> # A tibble: 1 × 2
#>   Item      N
#>   <fct> <int>
#> 1 saw       1
Chosen <- semi_join(DF, SingleVendor, by = "Item")
Chosen
#>   Item Vendor
#> 1  saw      3
#> 2  saw      3

Created on 2022-04-20 by the reprex package (v0.2.1)

1 Like

Thank you very much!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.