I'm in trouble... with an R script:
Given a tibble and a character vector
tbEsempio <- tibble(C1 = c(100,150,200,343,563), C2 = c("A", "A&B", "D", "D$C", "E"))
vcValori <- c("A","B","C")
I need filter tbEsempio on C2 with values in vcValori vector.
If I use %like%
without vector it is ok:
tbEsempioFiltered <- tbEsempio[which(tbEsempio$C2 %like% "A" |
tbEsempio$C2 %like% "B" |
tbEsempio$C2 %like% "C" )]
but using vcValori with %in%
operator instead %like%
tbEsempioFiltered <- tbEsempio %>% filter(C2 %in% {{vcValori}})
the line with 2 (or more) values separate by "&", in the exemple is 2 and 4, are not included.
There is a solution using tidyverse framework?
CodePudding user response:
You could use
library(dplyr)
tbEsempio %>%
filter(grepl(paste(vcValori, collapse = "|"), C2))
This returns
# A tibble: 3 x 2
C1 C2
<dbl> <chr>
1 100 A
2 150 A&B
3 343 D$C