Im looking to see if a string contains only blank space. The string could be
" "
or
" "
or
" "
etc...
I want to do this so I can change values in a data frame to NA, because my goal is to fix/clean messed up data.
Thank you
You can try with grepl:
grepl("^\\s*$", your_string)
"^\\s*$" asks for 0 or more (*) spaces (\\s) between beginning (^) and end ($) of string.
Examples
grepl("^\\s*$", " ")
#[1] TRUE
grepl("^\\s*$", "")
#[1] TRUE
grepl("^\\s*$", " ")
#[1] TRUE
grepl("^\\s*$", " ab")
[1] FALSE
NB: you can also just use a space instead of \\s in the regex ("^\\s*$").
Without regex, you could use
which(nchar(trimws(vec))==0)
The function trimws() removes trailing and leading whitespace characters from a string. Hence, if after the use of trimws the length of the string (determined by nchar()) is not zero, the string contains at least one non-whitespace character.
Example:
vec <- c(" ", "", " "," a ", " ", "b")
which(nchar(trimws(vec))==0)
#[1] 1 2 3 5
The entries 1, 2, 3, and 5 of the vector vec are either empty or contain only whitespace characters.
As suggested by Richard Scriven, the same result can be obtained without calling nchar(), by simply using trimws(vec)=="" (or which(trimws(vec)==""), depending on the desired output: the former results in a vector of booleans, the latter in the index numbers of the blank/empty entries).
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