I have a vector of names where some names have leading and trailing .
characters, and some do not. Here is an example:
test <- c('.name.1.','name.2','.name.3.')
I would like to conditionally remove leading and trailing .
characters in these names, to return
c('name.1','name.2','name.3')
A quick function using the
substr
function:We use
substr
to check for a.
in the first and last elements of the string, then we usesubstr
again to extract certain parts of the text. For example, if there is a.
in the first character, but not in the second, we'll extract:substr(text, 2, nchar(text))
.Use regular expressions:
The two backslashes,
\\
, in the regular expression escape the dot,.
, which would actually mean any character. The caret,^
, marks the beginning of the string, the dollar,$
, the end of the string. The,|
, is a logical "or". So in essence the regular expression matches a dot at the beginning of the string or a dot at the end of the string and replaces it with an empty string.More information on regular expressions can be found here and information on gsub and related functions here.
You can also use
str_extract
fromstringr
:or
str_replace_all
(stringr
-equivalent togsub
):Just for fun, here is a method with
substring
andgrepl
.This will work replacing
substring
withsubstr
. The cool thing about these functions is that they take vectors for their second and third arguments. Here, we can usegrepl
to increment between 1L and 2L for the second argument and between the position of the final character and the penultimate character.