[R] Regression expression to delete one or more spaces at end of string
marc_schwartz at me.com
Tue Aug 2 18:55:46 CEST 2016
> On Aug 2, 2016, at 11:46 AM, Dennis Fisher <fisher at plessthan.com> wrote:
> R 3.3.1
> OS X
> I have encountered an unexpected regex problem
> I have read an Excel file into R using the readxl package. Columns names are:
> COLNAMES <- c("Study ID", "Test and Biological Matrix", "Subject No. ", "Collection Date",
> "Collection Time", "Scheduled Time Point", "Concentration", "Concentration Units",
> "LLOQ", "ULOQ", "Comment”)
> As you can see, there is a trailing space in “Subject No. “. I would like to delete that space. The following works:
> sub(“ $”, “”, COLNAMES)
> However, I would like a more general approach that removes any trailing whitespace.
> I tried variations such as:
> sub("[:blank:]$", "", COLNAMES)
> (also, without the $ and ‘space' instead of ‘blank') without success — to my surprise, characters other than the trailing space were deleted but the trailing space remained.
> Guidance on the correct syntax would be appreciated.
There is actually an example in ?gsub:
## trim trailing white space
str <- "Now is the time "
sub(" +$", "", str) ## spaces only
The '+' sign will match the preceding space one or more times at the end of the character string.
Note that as per ?regex, it is [:space:], not [:blank:] and the brackets need to be doubled in the regex to define the enclosing character group. An example would be:
sub("[[:space:]]+$", "", str) ## white space, POSIX-style
which is also in ?gsub.
More information about the R-help