I have the dataframe of the following type
df <- tibble::tribble(~x,
c("A", "B"),
c("A", "B", "C"),
c("A", "B", "C", "D"),
c("A", "B"))
and vectors like these
vec1 <- c("A", "B")
vec2 <- c("A", "B", "C")
vec3 <- c("A", "B", "C", "D")
I want to mutate a variable y that shows which row has which vector. I tried the following, but getting the empty y variable with the warning: "longer object length is not a multiple of shorter object length"
df_new <- df %>%
mutate(y = case_when(x == vec1 ~ "vec1",
x == vec2 ~ "vec2",
x == vec2 ~ "vec3"))
The desired output is
df_new <- tibble::tribble(~x, ~y,
c("A", "B"), "vec1",
c("A", "B", "C"), "vec2",
c("A", "B", "C", "D"), "vec3",
c("A", "B"), "vec1")