[R] split a string a keep the last part

William Dunlap
Thu, 28 Aug 2014 18:20:25 -0000

Delete all characters up to and including the last hyphen with
     sub(".*-", "", test)
Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Thu, Aug 28, 2014 at 10:41 AM, Jun Shen <jun.shen.ut at gmail.com>
wrote:
> Hi everyone,
>
> I believe I am not the first one to have this problem but couldn't find
a
> relevant thread on the list.
>
> Say I have a string (actually it is the whole column in a data frame) in a
> format like this:
>
> test<- 'AF14-485-502-89-00235'
>
> I would like to split the test string and keep the last part. I think I can
> do the following
>
> sub('.*-.*-.*-.*-(.*)','\\1', test)
>
> to keep the fifth part of the string. But this won't work if other
strings
> have more or fewer parts separated by '-'. Is there a general way
to do it?
> Thanks.
>
> Jun
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.