So " xx yy 11 22  33 " will become "xxyy112233". How can I achieve this?
9 Answers
In general, we want a solution that is vectorised, so here's a better test example:
whitespace <- " \t\n\r\v\f" # space, tab, newline, 
                            # carriage return, vertical tab, form feed
x <- c(
  " x y ",           # spaces before, after and in between
  " \u2190 \u2192 ", # contains unicode chars
  paste0(            # varied whitespace     
    whitespace, 
    "x", 
    whitespace, 
    "y", 
    whitespace, 
    collapse = ""
  ),   
  NA                 # missing
)
## [1] " x y "                           
## [2] " ← → "                           
## [3] " \t\n\r\v\fx \t\n\r\v\fy \t\n\r\v\f"
## [4] NA
The base R approach: gsub
gsub replaces all instances of a string (fixed = TRUE) or regular expression (fixed = FALSE, the default) with another string.  To remove all spaces, use:
gsub(" ", "", x, fixed = TRUE)
## [1] "xy"                            "←→"             
## [3] "\t\n\r\v\fx\t\n\r\v\fy\t\n\r\v\f" NA 
As DWin noted, in this case fixed = TRUE isn't necessary but provides slightly better performance since matching a fixed string is faster than matching a regular expression.
If you want to remove all types of whitespace, use:
gsub("[[:space:]]", "", x) # note the double square brackets
## [1] "xy" "←→" "xy" NA 
gsub("\\s", "", x)         # same; note the double backslash
library(regex)
gsub(space(), "", x)       # same
"[:space:]" is an R-specific regular expression group matching all space characters.  \s is a language-independent regular-expression that does the same thing.
The stringr approach: str_replace_all and str_trim
stringr provides more human-readable wrappers around the base R functions (though as of Dec 2014, the development version has a branch built on top of stringi, mentioned below).  The equivalents of the above commands, using [str_replace_all][3], are:
library(stringr)
str_replace_all(x, fixed(" "), "")
str_replace_all(x, space(), "")
stringr also has a str_trim function which removes only leading and trailing whitespace.
str_trim(x) 
## [1] "x y"          "← →"          "x \t\n\r\v\fy" NA    
str_trim(x, "left")    
## [1] "x y "                   "← → "    
## [3] "x \t\n\r\v\fy \t\n\r\v\f" NA     
str_trim(x, "right")    
## [1] " x y"                   " ← →"    
## [3] " \t\n\r\v\fx \t\n\r\v\fy" NA      
The stringi approach: stri_replace_all_charclass and stri_trim
stringi is built upon the platform-independent ICU library, and has an extensive set of string manipulation functions.  The equivalents of the above are:
library(stringi)
stri_replace_all_fixed(x, " ", "")
stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")
Here "\\p{WHITE_SPACE}" is an alternate syntax for the set of Unicode code points considered to be whitespace, equivalent to "[[:space:]]", "\\s" and space().  For more complex regular expression replacements, there is also stri_replace_all_regex.
stringi also has trim functions.
stri_trim(x)
stri_trim_both(x)    # same
stri_trim(x, "left")
stri_trim_left(x)    # same
stri_trim(x, "right")  
stri_trim_right(x)   # same
 
    
    - 118,240
- 47
- 247
- 360
 
    
    - 18,516
- 4
- 48
- 45
- 
                    2@Aniko. Is there a reason you used fixed=TRUE? – IRTFM May 13 '11 at 12:57
- 
                    2@DWin Supposedly it is faster if R knows that it does not have to invoke the regular expression stuff. In this case it does not really make any difference, I am just in the habit of doing so. – Aniko May 13 '11 at 13:00
- 
                    1Is there a difference between `"[[:space:]]"` and `"\\s"`? – Sacha Epskamp May 13 '11 at 13:56
- 
                    @Sacha Good question, I don't know. If you are really interested, it might be worth creating a dedicated question to get the attention of somebody knowledgeable. – Aniko May 13 '11 at 14:19
- 
                    7if you check on http://flyordie.sin.khk.be/2011/05/04/day-35-replacing-characters/ or just type in ?regex then you see that [:space:] is used for "Space characters: tab, newline, vertical tab, form feed, carriage return, and space." That's a lot more than space alone – Sir Ksilem May 13 '11 at 14:25
- 
                    1@Aniko Hope you don't mind about the big edit. Since this question is highly popular, it looked like the answer needed to be more thorough. – Richie Cotton Dec 31 '14 at 10:52
- 
                    Bookmarking this post for future review. Thanks for such a comprehensive comment. – Matt Barger May 09 '18 at 19:50
- 
                    For those who are still struggling despite this correct answer. Mind that you can have non-breaking space which is a different character. Use `gsub("\u00A0", "", x)` to get rid of these. – qfazille May 11 '20 at 15:45
I just learned about the "stringr" package to remove white space from the beginning and end of a string with str_trim( , side="both") but it also has a replacement function so that:
a <- " xx yy 11 22 33 " 
str_replace_all(string=a, pattern=" ", repl="")
[1] "xxyy112233"
 
    
    - 473
- 4
- 9
- 
                    4stringr package doesn't work well with every encoding. stringi package is better solution, for more info check https://github.com/Rexamine/stringi – bartektartanus Feb 20 '14 at 10:59
Use [[:blank:]] to match any kind of horizontal white_space characters.
gsub("[[:blank:]]", "", " xx yy 11 22  33 ")
# [1] "xxyy112233"
 
    
    - 172,303
- 28
- 230
- 274
Please note that soultions written above removes only space. If you want also to remove tab or new line use stri_replace_all_charclass from stringi package.
library(stringi)
stri_replace_all_charclass("   ala \t  ma \n kota  ", "\\p{WHITE_SPACE}", "")
## [1] "alamakota"
 
    
    - 12,836
- 2
- 50
- 75
 
    
    - 15,284
- 6
- 74
- 102
- 
                    5
- 
                    1This command above is incorrect. The right way is stri_replace_all_charclass(" ala \t ma \n kota ", "\\p{WHITE_SPACE}", "") – Lucas Fortini Aug 07 '14 at 02:10
- 
                    5After using `stringi` for a few months now and seen/learned how powerful and efficient it is, it has become my go-to package for string operations. You guys did an awesome job with it. – Rich Scriven Dec 29 '14 at 01:41
The function str_squish() from package stringr of tidyverse does the magic!
library(dplyr)
library(stringr)
df <- data.frame(a = c("  aZe  aze s", "wxc  s     aze   "), 
                 b = c("  12    12 ", "34e e4  "), 
                 stringsAsFactors = FALSE)
df <- df %>%
  rowwise() %>%
  mutate_all(funs(str_squish(.))) %>%
  ungroup()
df
# A tibble: 2 x 2
  a         b     
  <chr>     <chr> 
1 aZe aze s 12 12 
2 wxc s aze 34e e4
 
    
    - 11,659
- 11
- 40
- 58
 
    
    - 475
- 5
- 6
- 
                    4Please do not link to code. Add it in the text body of your answer and explain it here, to give your answer more longterm value. – R Balasubramanian Aug 07 '18 at 14:04
- 
                    Thanks @RBalasubramanian for reminding me of this guideline. I will follow it in the future. – damianooldoni Aug 09 '18 at 08:26
- 
                    3I don't see how this answers the question. `str_squish` doesn't remove all spaces. It just trims and substitutes multiple spaces for one. – Nettle Aug 16 '18 at 21:01
Another approach can be taken into account
library(stringr)
str_replace_all(" xx yy 11 22  33 ", regex("\\s*"), "")
#[1] "xxyy112233"
\\s: Matches Space, tab, vertical tab, newline, form feed, carriage return
*: Matches at least 0 times
 
    
    - 1,304
- 2
- 8
- 20
- 
                    Should be noted that the regex() command also comes from the stringr package. – Sky Scraper Dec 22 '22 at 21:30
income<-c("$98,000.00 ", "$90,000.00 ", "$18,000.00 ", "")
To remove space after .00 use the trimws() function.
income<-trimws(income)
 
    
    - 27,810
- 13
- 101
- 139
 
    
    - 31
- 2
From stringr library you could try this:
- Remove consecutive fill blanks
- Remove fill blank - library(stringr) - 2. 1. | | V V str_replace_all(str_trim(" xx yy 11 22 33 "), " ", "")
 
    
    - 9
- 2
 
     
     
    