1

In R, if you want to trim all whitespace characters from the end of a string, you'd do trimws(to.be.trimmed, "right"), like so:

> trimws("nauris        ", "right")
[1] "nauris"

What if instead of trimming out whitespace characters, I'd want to trim periods (or some other characters)? In Python, you'd do string.rstrip(char). Here's some desired outputs:

> rstrip("nauris", "s")
[1] "nauri"
> rstrip("nauris.", ".")
[1] "nauris"
> rstrip("nauris....", ".")
[1] "nauris"
> rstrip("stack", "c")
[1] "stack"

Giving said period as the last argument does not work as it just returns an empty string:

trimws("nauris.", "right", ".")
[1] ""
hilssu
  • 416
  • 4
  • 18
  • Is it always the same character each time you are trimming? Would that character appear elsewhere in any of the strings? – Jaccar Jun 19 '19 at 10:08
  • Why not use `gsub` – Jason Mathews Jun 19 '19 at 10:09
  • gsub removes those characters from the middle of the string as well, for example gsub('\\c', '', "stack") returns "stak" instead of "stack". – hilssu Jun 19 '19 at 10:10
  • Related to https://stackoverflow.com/questions/53754273/extract-characters-bound-by-parentheses/53754556#53754556 – akrun Jun 19 '19 at 15:08

2 Answers2

6

From R version 3.6.0 trimws() has a whitespace argument so you can do:

trimws("nauris", "right", whitespace = "s")
[1] "nauri"

trimws("nauris.....", "right", whitespace = "\\.")
[1] "nauris"

The documentation states that internally trimws() uses sub(re, "", *, perl = TRUE) so special characters need to be escaped.

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
1

For people who haven't updated R yet (like me), you can copy the trimws function and change it according to our requirement.

trim_periods <- function (x, which = c("both", "left", "right")) {
   which <- match.arg(which)
   mysub <- function(re, x) sub(re, "", x, perl = TRUE)
  if (which == "left") 
     return(mysub("^[.]+", x))
   if (which == "right") 
      return(mysub("[.]+$", x))
   mysub("[.]+$", mysub("^[.]+", x))
}

trim_periods("...abc..def..", "right")
#[1] "...abc..def"

trim_periods("...abc..def..")
#[1] "abc..def"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213