tibble(mytext = list(list('rstudio is nice', 'rstats is cool'),
list('this stuff is incredible', 'hello world')))
# A tibble: 2 x 1
mytext
<list>
1 <list [2]>
2 <list [2]>
For each row, I would like to do two things
only keep the elements of the the mytext list that contain rstudio
create a variable that is TRUE is any of these mytext elements contain rstudio.
I am able to do 1. with purrr::keep but the second one fails with purrr::some.
tibble(mytext = list(list('rstudio is nice', 'rstats is cool'),
list('this stuff is incredible', 'hello world'))) %>%
mutate(subsample = map(mytext, ~purrr::keep(.x, str_detect(.x,'rstudio')))) %>%
mutate(flag = purrr::some(mytext, ~str_detect(.x,'rstudio')))
# A tibble: 2 x 3
mytext subsample flag
<list> <list> <lgl>
1 <list [2]> <list [1]> TRUE
2 <list [2]> <list [0]> TRUE
As you can see, subsample correctly subsets the lists, while flag returns TRUE for all rows while it should only be true for row 1 ... Indeed, only the first row of the variable mytext contains a list that contains the string rstudio.