Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditionally remove leading or trailing `.` character in R

Tags:

regex

r

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')
like image 202
colin Avatar asked Oct 10 '17 16:10

colin


1 Answers

Use regular expressions:

test <- c('.name.1.','name.2','.name.3.')
gsub('^\\.|\\.$', '', test)
# [1] "name.1" "name.2" "name.3"

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 pipe, |, 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.

like image 55
guscht Avatar answered Oct 05 '22 04:10

guscht