As you can see from the examples below, column A has multiple elements of different lengths. I'm trying to use case_when within a mutate call to create categorical columns that 'tag' subsets of these multiple elements.
For example, we create a dummy dataset:
df <- tribble(
~A, ~B,
c("a", "b", "c", "1", "2"), "alpha",
c("b", "c", "d", "1"), "beta",
c("c", "d", "1", "2", "3"), "gamma"
)
And then to evaluate subset membership I'm using any and all over an %in% call, like so:
result <- df %>%
mutate(
test = case_when(all(c("a","1") %in% A) ~ "x",
any(c("beta","gamma") %in% B) ~ "y")
)
Here, I'm expecting result$test to show x, y, y. Instead I'm getting NAs.
Is my approach possible? If not, what's the reason and what's the most efficient alternative strategies I could implement?