Reputation: 45
I have a nested list videos inside the sublist there is element "title" I want to filter and remove all the sublists in which x$title has the words like {trailer, highlights, match}. Can some good soul help me in solving this ?
(Sorry for my language) Thanks in advance
Upvotes: 0
Views: 413
Reputation: 4419
Making a small sample dataset (See this SO post).
l <- list(
list(id = "a", title = "blabla trailer"),
list(id = "b", title = "keep this one"),
list(id = "c", title = "remove this match"))
To subset list elements based on search patterns we can use some regular expression to find matches those matches. We can use |
to search for multiple possibilities.
# base R
l[!sapply(l, function(x){grepl("match|highlight|trailer", x$title)})]
# purrr
library(purrr)
l[!map_lgl(l, ~ grepl("match|highlight|trailer", .x$title))]
[[1]]
[[1]]$id
[1] "b"
[[1]]$title
[1] "keep this one"
Upvotes: 0
Reputation: 694
Find all the sublists x
for which x$title
contains any of the forbidden words and remove them.
forbidden <- c("trailer", "highlights", "match")
bad <- sapply(videos, function(x) any(stringr::str_detect(x$title, regex(forbidden, ignore_case = T))))
videos <- videos[-which(bad)]
Upvotes: 1