[minor edits for grammar and readability]
I'm trying to filter a data set that has a list as one of the variables. Is there a way to do this elegantly with the tidyverse, if no, what's a reasonable way of doing this?
library(dplyr)
color_df <- tibble(
color = c("red", "orange", "yellow", "green",
"blue", "indigo", "violet")
)
base_colors <- list("red", c("red", "yellow"), "yellow",
c("yellow", "blue"), "blue", "blue",
c("blue", "red")
)
color_df$color_base <- base_colors
color_df %>% filter("red" %in% color_base)
Ideally, the last line would return a df with the 3 rows where color was red, orange, and purple, instead of just returning the whole data frame (I'm assuming this is because "red"
is in the color_base
variable).
Is there any way of doing this?