[R] split a string a keep the last part
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@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.
Re: [R] split a string a keep the last part
Here's one way: R test- 'AF14-485-502-89-00235' R test2 - strsplit(test, -) R test2 [[1]] [1] AF14 485 502 8900235 R test2[[1]][length(test2[[1]])] [1] 00235 On Thu, Aug 28, 2014 at 1:41 PM, Jun Shen jun.shen...@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 -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] split a string a keep the last part
On Aug 28, 2014, at 12:41 PM, Jun Shen jun.shen...@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 Try this: test - 'AF14-485-502-89-00235' sub(^.*-(.*)$, \\1, test) [1] 00235 test - 'AF14-485-502-89-00235-1234' sub(^.*-(.*)$, \\1, test) [1] 1234 Another option: tail(unlist(strsplit(test, -)), 1) [1] 1234 Regards, Marc Schwartz __ R-help@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.
Re: [R] split a string a keep the last part
On Thu, 28 Aug 2014, Jun Shen jun.shen...@gmail.com writes: 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 This should work for your example: gsub(.*-([^-]*)$, \\1, test) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net __ R-help@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.
Re: [R] split a string a keep the last part
Thanks for everyone who replied with those wonderful solutions! Jun On Thu, Aug 28, 2014 at 2:11 PM, Enrico Schumann e...@enricoschumann.net wrote: On Thu, 28 Aug 2014, Jun Shen jun.shen...@gmail.com writes: 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 This should work for your example: gsub(.*-([^-]*)$, \\1, test) -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net [[alternative HTML version deleted]] __ R-help@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.
Re: [R] split a string a keep the last part
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...@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@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. __ R-help@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.