0

I'm trying to fix some cells in a tibble. The column of my interest contains strings with white spaces and double white spaces at begin or end.

I saw these posts before asking

This is a reproducible example of what I'm doing

library(dplyr)

mtcars2 = tbl_df(mtcars) %>% 
   mutate(name = rownames(mtcars)) %>%
   mutate(name = gsub("^ *|(?<= ) | *$", "", name, perl = TRUE)) %>%  
   mutate(name = gsub("^\\s+|\\s+$", "", name)) %>%
   mutate(name = iconv(name, from = "", to = "ASCII//TRANSLIT", sub = ""))

head(mtcars2, 3)

And the result is

# A tibble: 3 × 12
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb          name
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>         <chr>
1  21.0     6   160   110  3.90 2.620 16.46     0     1     4     4     Mazda RX4
2  21.0     6   160   110  3.90 2.875 17.02     0     1     4     4 Mazda RX4 Wag
3  22.8     4   108    93  3.85 2.320 18.61     1     1     4     1    Datsun 710

But in my dataset after doing that some double white spaces persist !!

Is there a more general command to remove white spaces at the end of string? Many thanks in advance !

Community
  • 1
  • 1
pachadotdev
  • 3,345
  • 6
  • 33
  • 60
  • 1
    Where are those whitespaces? Are these regular or some Unicode whitespaces? Try `gsub("^\\s+|\\s+$|(\s)+", "$1", name)` – Wiktor Stribiżew Apr 21 '17 at 19:18
  • "But in my dataset after doing that some double white spaces persist" -- in other words, you don't have a reproducible example yet? There is `trimws`, probably mentioned in your links, though I don't see it in your code. – Frank Apr 21 '17 at 19:22

1 Answers1

5

Have you tried the trimws() function?

> trimws('   hello   ')
[1] "hello"
Tim Atreides
  • 151
  • 3
  • 1
    https://stat.ethz.ch/R-manual/R-devel/library/base/html/trimws.html amazing !! I didn't know that and it also considers tabs as whitespace !! – pachadotdev Apr 21 '17 at 19:25
  • `mutate(name = gsub(" ", "", name)) %>%` seems to work ... for now :( – pachadotdev Apr 21 '17 at 19:29
  • `gsub`ing will remove trailing white spaces, but it will also remove spaces inside the string, which could cause worse problems. – Tim Atreides Apr 22 '17 at 15:32