I am trying to filter a tibble to the rows where it contains a certain value in a column. However, this column has more than one values that are separated by a ";".
For example,
A
<chr>
1 "this; is; one; value"
2 "this; is; another"
Let's say I want to filter this tibble and get the rows where at least one of the words is "one". However, I couldn't find a way to do so.
I tried to split these values based on ;, unlist them, and then check but it didn't work. My code was something like:
tibble %>%
filter(unlist(str_split(A, ";")) == "one")
But this raised an error. Could someone help me?
CodePudding user response:
In the tidyverse we could use stringr::str_detect
inside dplyr::filter
:
library(dplyr)
library(stringr)
tibble(A = c("this; is; one; value", "this; is; another")) %>%
filter(str_detect(A, "one"))
#> # A tibble: 1 x 1
#> A
#> <chr>
#> 1 this; is; one; value
Created on 2021-09-26 by the reprex package (v0.3.0)
And in base R we can do:
dat <- data.frame(A = c("this; is; one; value", "this; is; another"))
subset(dat, grepl("one", A))
#> A
#> 1 this; is; one; value
Created on 2021-09-26 by the reprex package (v0.3.0)
CodePudding user response:
I am no expert in R, but I guess you could just do:
filter(df, str_detect(df$A, "one;"))
Output:
A
1 1 this; is; one; value