b <- c("books", " ", "animals", "frogs")
#My code:
b[!grepl("^\\s+$", b)]
[1] "books" "animals" "frogs"
#Now, I am working to figure out this solution with stringr package.
str_remove_all(b, "^\\s+$")
[1] "books" "" "animals" "frogs"
The output shows ""
where my new code fails. Any solution to get the result like my first code?
Given a String and a Word, the task is remove that Word from the String. Approach : In Java, this can be done using String replaceAll method by replacing given word with a blank space. string string1 = "Geeks for Geeks.";
One of the typical tasks of string processing is that of parsing a text into individual words. Usually, you end up with words that have blank spaces, called whitespaces, on either end of the word. In this situation, you can use the str_trim () function to remove any number of whitespaces at the ends of a string.
If we want to remove all “c” from our string, we need to use the str_remove_all command. So let’s move on to the next example… We can eliminate all “c” from our character string with the str_remove_all function as shown below:
The length of the string is six. The following statement uses the TRIM function with the LEADING option to remove all leading spaces of the string. You can test it by using the LENGTH function. The length of the result string must be four because the TRIM function removes two spaces at the beginning of the string.
We may use str_subset
in stringr
library(stringr)
str_subset(b, "^\\s+$", negate = TRUE)
[1] "books" "animals" "frogs"
The function that corresponds to grepl
is str_detect
b[str_detect(b, "^\\s+$", negate = TRUE)]
[1] "books" "animals" "frogs"
In base R
, we may use grep
with invert = TRUE
grep("^\\s+$", b, invert = TRUE, value = TRUE)
[1] "books" "animals" "frogs"
Or without regex with trimws
(to remove the spaces - leading/lagging) and use nzhcar
to create logical vector for subsetting
b[nzchar(trimws(b))]
[1] "books" "animals" "frogs"
In base R
we can do:
b[b !=" "]
Output:
[1] "books" "animals" "frogs"
OR with stringr
:
library(stringr)
str_subset(str_squish(b), "")
[1] "books" "animals" "frogs"
Two base R alternatives:
b[nzchar(trimws(b))]
# [1] "books" "animals" "frogs"
b[grepl("\\S",b)]
# [1] "books" "animals" "frogs"
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