I have a data frame that has two columns, a
and b
. Both a
and b
have vectors of different lengths, and I want to create another column, called boolean
, which should be true when at least one value from the vector in b
is present in the vector in column a
. However, I used the following logic and I'm getting a false
for all rows, even though I should be getting true
for rows 1, 3 and 4. A reproducible example of my attempt is as follows:
library(tidyverse)
library(tibble)
data_frame <-
tribble(
~a, ~b,
c("50", "50", "50", "3"), c("1", "2", "3"),
c("17", "50", "50", "50"), "19",
c("50", "28", "50", "3"), c("30", "28", "29"),
c("21", "19", "50", "50", "50"), c("19", "20", "18")
)
data_frame %>%
mutate(
boolean = if_else(
any(b %in% a),
"yes",
"no"
)
)
#> # A tibble: 4 x 3
#> a b boolean
#> <list> <list> <chr>
#> 1 <chr [4]> <chr [3]> no
#> 2 <chr [4]> <chr [1]> no
#> 3 <chr [4]> <chr [3]> no
#> 4 <chr [5]> <chr [3]> no
Is there something wrong with how I am using the any
command? Thanks.