Checking for membership in a column of list vectors
Solution 1:
You could use lapply/sapply to test the string on all of the members of df$Result.
testString <-"Staphylococcus chromogenes"
sapply(df$Result, function(results){testString %in% results})
#[1] TRUE FALSE FALSE
Solution 2:
Try grepl
+ toString
> grepl("Staphylococcus chromogenes", sapply(df$Result, toString), fixed = TRUE)
[1] TRUE FALSE FALSE
Solution 3:
Another possible solution, based on dplyr
:
library(dplyr)
df %>%
rowwise %>%
mutate(Presence = "Staphylococcus chromogenes" %in% Result)
#> # A tibble: 3 × 4
#> # Rowwise: Site, CowId
#> Site CowId Result Presence
#> <fct> <int> <list> <lgl>
#> 1 FarmA 1000 <chr [2]> TRUE
#> 2 FarmA 1001 <chr [1]> FALSE
#> 3 FarmA 1002 <chr [1]> FALSE
Solution 4:
Another option is to use tidyverse
. Here, I mutate
a new column so that you can see where the taxa occurs. I use str_detect
within map
to check to see if the string occurs within a given list, then return TRUE
if the string occurs at all in a given list (i.e., using any
).
library(tidyverse)
df %>%
mutate(taxa_present = map_lgl(Result, function(v)
str_detect(v, "Staphylococcus chromogenes") %>% any()))
Output
# A tibble: 3 × 4
# Groups: Site, CowId [3]
Site CowId Result taxa_present
<fct> <int> <list> <lgl>
1 FarmA 1000 <chr [2]> TRUE
2 FarmA 1001 <chr [1]> FALSE
3 FarmA 1002 <chr [1]> FALSE
Or if you just want a simple logical vector, then you could just do:
map_lgl(df$Result, function(v)
str_detect(v, "Staphylococcus chromogenes") %>% any())
#[1] TRUE FALSE FALSE