0

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 ?

Here is the Nested List in R

Here is the Sublist

(Sorry for my language) Thanks in advance

2 Answers2

1

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)]
slowowl
  • 664
  • 1
  • 13
  • I tried this videos <- videos[!sapply(videos, function(x) any(stringr::str_detect(x$title, forbidden)))] from what you suggested and it worked but it is case sensative how can I opt for no case sensative ? – Ked NIMBALKAR Feb 06 '22 at 05:10
  • So I got it like this videos <- videos[!sapply(videos, function(x) any(stringr::str_detect(x$title, regex((forbidden),ignore_case = TRUE))))] – Ked NIMBALKAR Feb 06 '22 at 05:44
  • Yes, I found it too. I am updating my response. – slowowl Feb 06 '22 at 05:49
0

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"
Donald Seinen
  • 4,179
  • 5
  • 15
  • 40