Removing Space Between Numeric Values in R

Removing space between numeric values in R

Try using gsub with lapply to remove all whitespace:

BinToDec <- function(x) 
sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1))

lst <- list("1 1 1 0 1 1", "0 0 0 0 1 0", "0 0 0 0 1 0")
unlist(lapply(lst, function(x) BinToDec(gsub("\\s+", "", x))))

[1] 59 2 2

I took the above BinToDec function from this SO answer, which maybe is also where you saw it.

Edit:

If you actually have a list of integer vectors, then use this option:

lst <- list(c(1,1,1,0,1,1), c(0,0,0,0,1,0), c(0,0,0,0,1,0))
unlist(lapply(lst, function(x) BinToDec(paste(as.character(x), collapse=""))))

[1] 59 2 2

Demo

Remove spaces between a word and a number

text=c("There is a cooling problem in the component tx 313","leakage found in irc 256, fixed by replacement",
"Roasted cable in cpt trx235","word 123 456")

gsub("(?<=[a-z]) (?=\\d)","",text,perl = T)
[1] "There is a cooling problem in the component tx313" "leakage found in irc256, fixed by replacement"
[3] "Roasted cable in cpt trx235" "word123 456"

(?<=[a-z]) positive lookbehind to check if there is a letter before what needs to be replaced.

what we want to remove, a space.

(?=\\d) positive lookahead to check that the space is followed by a digit.

Remove spaces and convert values to as numeric when only numbers in R

I built a function using a regex

library(tidyverse)
mClean <- function(strVec){
pass1 <- strVec %>%
str_trim() %>%
str_extract("(?x) # Perl-style whitespace
^[\\+\\-]? # An optional leading +/-
\\d+ # the integer part
(\\.\\d+)? # A fractional part
") %>%
as.numeric()
}

I put your data in a tibble and ran it:

df <- tibble('Col1' = c('421', ' 0.52', '-0.88 ', '1.2 (ref)', ' 97  '),
'Col2' = c('0.0', '0.27,0.91', '3.0', ' 10242.3', ' 94.5')) %>%
mutate(cln1 = as.numeric(mClean(Col1)),
cln2 = as.numeric(mClean(Col2)))
df

# A tibble: 5 x 4
Col1 Col2 cln1 cln2
<chr> <chr> <dbl> <dbl>
1 421 0.0 421 0
2 " 0.52" 0.27,0.91 0.52 0.27
3 "-0.88 " 3.0 -0.88 3
4 1.2 (ref) " 10242.3" 1.2 10242.
5 " 97 " " 94.5" 97 94.5

I wasn't sure what you wanted done with that '0.27,0.91'. Break it into two rows? Make another column for the '0.91'? Anyway, this keeps the original input in the same row as the cleaned up values.

How to remove spaces between words in R?

We may capture the letter without the space and use the backreference (\\1, \\2) in gsub

gsub("([A-Za-z])\\s+([A-Za-z])", "\\1\\2", string)
[1] "Hibuddy 3.256.225 25.365.425"

The above regex matches any letter ([A-Za-z]) capture as a group ((...)), followed by one or more spaces (\\s+) and another letter, which is captured as a group as well. In the replacement, specify the backreferences of those captured groups without any spaces

How to remove all whitespace from a string?

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

Remove spaces between digits without gluing separate float numbers together

You may use

library(stringr)
x <- c("1 9.7 1 0.8", "7.6 7.5", "3.7 13.5", "8.6 1 5.8")
str_replace_all(x, "\\d(?:\\s*\\d)*\\.\\d+", function(z) str_replace_all(z, "\\s+", ""))
# => [1] "19.7 10.8" "7.6 7.5" "3.7 13.5" "8.6 15.8"

See the R demo online and the regex demo.

Regex details

  • \d - a digit
  • (?:\s*\d)* - 0 or more occurrences of 0 or more whitespaces followed with a digit
  • \. - a dot
  • \d+ - one or more digits.

All whitespaces are removed from each match using a mere str_replace_all(z, "\\s+", "").

how to delete a space between a number and a character using r and stringr

We can do this with a single capture group using sub

sub("(\\d+)\\s+", "\\1", str1)
#[1] "xxxxxx xxxx xxxxxx 1.5L xxxxx" "xxxxxx xxxx xxxxxx 1.5L xxxxx"

data

str1 <- c("xxxxxx xxxx xxxxxx 1.5L xxxxx" , "xxxxxx xxxx xxxxxx 1.5 L xxxxx")

How to remove space before dot using paste() function in R

Paste0(...) or paste(..., sep = "") work fine, but I find the glue() offers a clean way of concatenating strings, without lots of opening and closing quotes.

In this case:

glue::glue("The values are {a} and {b}.")

How to remove whitespace between numbers only?

You can use

$_.name -replace '[^\w\s]*(\d+)\W*(?=[\d\W]*$)', '$1'

See the regex demo

Details

  • [^\w\s]* - any zero or more chars other than word and whitespace chars
  • (\d+) - Capturing group 1 ($1 refers to this value from the replacement pattern): one or more digits
  • \W* - any zero or more non-word chars...
  • (?=[\d\W]*$) - that are immediately followed with zero or more non-word or digit chars up to the end of string.


Related Topics



Leave a reply



Submit