Detecting if multiple characters are found within group of string vectors

One dplyr and purrr option could be:

df1 %>%
 bind_cols(map_dfc(.x = vec1,
                   ~ df1 %>%
                    transmute(!!.x := str_detect(.x, a) & str_detect(.x, b))) %>%
            transmute(c = rowSums(select(., everything())) >= 1))

   a b     c
1  Q M FALSE
2  S H FALSE
3  S R FALSE
4  S H FALSE
5  W C FALSE
6  W F  TRUE
7  W I FALSE
8  W K FALSE
9  W L FALSE
10 W M FALSE
11 W P FALSE
12 W Q FALSE
13 W R FALSE
14 W V FALSE
15 W Y  TRUE
16 N F FALSE
17 N I FALSE
18 N M FALSE
19 N T FALSE
20 N V FALSE
21 N W FALSE
22 N Y FALSE
23 A M FALSE
24 N C FALSE
25 Q F FALSE
26 Q I FALSE
27 Q L FALSE
28 Q V FALSE
29 W F  TRUE
30 W I FALSE
31 W L FALSE
32 W T FALSE
33 W V FALSE
34 W Y  TRUE
35 D C FALSE
36 T V FALSE
37 S N FALSE
38 S Y FALSE

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top