sjmisc icon indicating copy to clipboard operation
sjmisc copied to clipboard

Use str_contains for dataframe?

Open bes827 opened this issue 4 years ago • 2 comments

str_contains is a great function. is it possible to use for detecting a string in a column in a dataset? is there a way for x to be a variable? eg dataset$variable1, rather than a vector or a string? thank you

bes827 avatar Sep 26 '20 05:09 bes827

dataset$variable1 is a vector, so yes, this should work. You should place the search pattern as first, the column / variable as second argument:

library(sjmisc)
d <- data.frame(v1 = c("def", "abc", "xyz"))
str_contains("abc", d$v1, switch = TRUE)
#> [1] FALSE  TRUE FALSE

Created on 2020-10-30 by the reprex package (v0.3.0)

strengejacke avatar Oct 30 '20 10:10 strengejacke

thanks you. this works. do I have to keep switch = TRUE in this case?

bes827 avatar Oct 31 '20 01:10 bes827