I have a tibble tib
as follows:
A B C D
<chr> <chr> <chr> <chr>
1 X123 X456 K234 V333
2 X456 Z000 L888 B323
3 X789 ZZZZ D345 O999
4 M111 M111 M111 M111
.
.
.
(5000 rows)
I also have another vector as follows:
> vec <- c("X123","X456")
> vec
[1] "X123" "X456"
I am looking for a way to search for, and add a logical column (with 5000 rows, in the e.g.) to the right of the tibble that is either TRUE
or FALSE
depending on whether any of the values of the columns in tib
contain a value in vec
. My goal output is the following:
A B C D lgl
<chr> <chr> <chr> <chr> <lgl>
1 X123 X456 K234 V333 TRUE
2 X456 Z000 L888 B323 TRUE
3 X789 ZZZZ D345 O999 FALSE
4 M111 M111 M111 M111 FALSE
I have the following:
> tib %>%
+ pmap_lgl(~any(..1 %in% vec))
[1] TRUE TRUE FALSE FALSE
This gets the results that I am seeking, but I'm a bit confused about the syntax.
Why does the above work (i.e. using ..1
), instead of having to use ..1
, ..2
, ..3
, and ..4
? My understanding is that pmap
generates a vector based on the inputs rowwise, so I assume that ..1
in the above means the vector c("X123","X456","K234","V333")
for row #1, c("X456","Z000","L888","B323")
for row #2, etc.
In the end, I have two questions:
- How do I append this new logical vector to the above
tib
? I haven't had any luck with:
tib %>% mutate(lgl = pmap_lgl(~any(..1 %in% vec)))
Error in mutate_impl(.data, dots): Evaluation error: argument ".f" is missing, with no default.
- If I were to watch to access each column within each row (e.g. "X123" for the first row in pmap), how would I do that within the syntax of
purrr
?