truncate string from a certain character in R

user802231 picture user802231 · Jul 28, 2011 · Viewed 43k times · Source

I have a list of strings in R which looks like:

WDN.TO
WDR.N
WDS.AX
WEC.AX
WEC.N
WED.TO

I want to get all the postfix of the strings starting from the character ".", the result should look like:

.TO
.N
.AX
.AX
.N
.TO

Anyone have any ideas?

Answer

Tommy picture Tommy · Jul 29, 2011

Joshua's solution works fine. I'd use sub instead of gsub though. gsub is for substituting multiple occurrences of a pattern in a string - sub is for one occurrence. The pattern can be simplified a bit too:

> x <- c("WDN.TO","WDR.N","WDS.AX","WEC.AX","WEC.N","WED.TO")
> sub("^[^.]*", "", x)
[1] ".TO" ".N"  ".AX" ".AX" ".N"  ".TO"

...But if the strings are as regular as in the question, then simply stripping the first 3 characters should be enough:

> x <- c("WDN.TO","WDR.N","WDS.AX","WEC.AX","WEC.N","WED.TO")
> substring(x, 4)
[1] ".TO" ".N"  ".AX" ".AX" ".N"  ".TO"