I exploded items from two categories (basket and shelf) using separate into separate columns, with one item per column. Now, I am trying to count the number of matches between the two categories, regardless of which column order they are in. I thought about using map2_lgl in a mutate but I am not sure how to get it to work and how to broadcast each column to all other columns in the other category. Appreciate any advice!
df <- tibble::tribble(
~basket_1, ~basket_2, ~basket_3, ~shelf_1, ~shelf_2, ~shelf_3,
"apple", "orange", "mango", "orange", "mango", NA,
"milk", "sausage", "beef", "beef", "coke", "towel",
"batteries", "lighter", "gasoline", "lighter", "batteries", "gasoline"
)
df
# number of matches: 2, 1, 3
# A tibble: 3 x 6
basket_1 basket_2 basket_3 shelf_1 shelf_2 shelf_3
<chr> <chr> <chr> <chr> <chr> <chr>
1 apple orange mango orange mango NA
2 milk sausage beef beef coke towel
3 batteries lighter gasoline lighter batteries gasoline
# I thought about something like this (of course, this does not work):
df %>% mutate(matches = map2_lgl(starts_with("basket_"),
starts_with("shelf_"),
~str_detect(.x, .y) %>% sum())
Would be great to extract the column names then convert the vector of strings to a vector of objects. I tried sapply(df %>% select(starts_with("basket_")) %>% colnames(), get) and eval(parse(text = df %>% select(starts_with("basket_")) %>% colnames()) to no avail though.