[R] Splitting the string at the last sub-string
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Sep 15 15:29:28 CEST 2005
> regexpr(".*e", str)
tells you you need
> substring(str, c(1, 26), c(25,length(str)))
 "Chance favors the prepare" "d mind"
to reproduce your answer (I don't know what you want to do with the
substring, but you included it in the first string, which is not what
On Thu, 15 Sep 2005, Tuszynski, Jaroslaw W. wrote:
> I need to split a string into 2 strings, with the split point defined by the
> last occurrence of some substring. I come up with some convoluted code to do
> str = "Chance favors the prepared mind"
> sub = "e"
> y = unlist(strsplit(str,sub))
> z = cbind(paste(y[-length(y)], sub, sep="", collapse = ""), y[length(y)]);
> Is there a simpler way to do so? I think ~8 function calls to do such a
> simple operation is an overkill.
> Jarek Tuszynski, PhD. o / \
> Science Applications International Corporation <\__,|
> (703) 676-4192 "> \
> Jaroslaw.W.Tuszynski at saic.com ` \
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help