I would like to use str_detect and not convert "" to another string pattern. Is there an easy way to deal with empty string patterns "" which right now generates a warning. I would like this to produce TRUE, FALSE, FALSE, FALSE, FALSE
library( tidyverse )
str_detect('matt', c( "matt","joe","liz","", NA))
In this shot, we will learn to check if a string is empty or not. In Golang, we can do this by: Comparing it with an empty string. Evaluating the length of the string; if it's zero, then the string is empty, and vice versa.
Empty strings contain zero characters and display as double quotes with nothing between them ( "" ). You can determine if a string is an empty string using the == operator.
Strings are immutable in Python, you cannot add to them. You can, however, concatenate two or more strings together to make a third string.
It is valid to have a string of zero characters, written just as '' , called the "empty string". The length of the empty string is 0. The len() function in Python is omnipresent - it's used to retrieve the length of every data type, with string just a first example.
We can use
library(stringr)
library(tidyr)
str_detect(replace_na(v1, ''), 'matt')
#[1] TRUE FALSE FALSE FALSE FALSE
If the match is not for a substring, then %in%
would be useful
v1 %in% 'matt'
#[1] TRUE FALSE FALSE FALSE FALSE
v1 <- c( "matt","joe","liz","", NA)
If you're not tied to str_detect()
perhaps try grepl()
?
grepl("matt", c( "matt","joe","liz","", NA))
#[1] TRUE FALSE FALSE FALSE FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With