Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stringr functions to remove all empty words?

Tags:

r

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?

like image 588
Ibrahimli Avatar asked Oct 17 '21 19:10

Ibrahimli


People also ask

How to remove a word from string in Java?

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.";

How to remove blank spaces at the end of a string?

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.

How to remove all “C” from a string in Python?

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:

How to remove all leading spaces from a string in Python?

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.


3 Answers

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"  
like image 156
akrun Avatar answered Oct 10 '22 00:10

akrun


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" 
like image 1
TarJae Avatar answered Oct 10 '22 01:10

TarJae


Two base R alternatives:

b[nzchar(trimws(b))]
# [1] "books"   "animals" "frogs"  
b[grepl("\\S",b)]
# [1] "books"   "animals" "frogs"  
like image 2
r2evans Avatar answered Oct 10 '22 01:10

r2evans