[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)
[1] 1
attr(,"match.length")
[1] 25

tells you you need

> substring(str, c(1, 26), c(25,length(str)))
[1] "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 
split() does).

On Thu, 15 Sep 2005, Tuszynski, Jaroslaw W. wrote:

>
> Hi,
>
> 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
> so:
>
> 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)]);
>
> y
> z
> z[1]
> z[2]
>
> Is there a simpler way to do so? I think ~8 function calls to do such a
> simple operation is an overkill.
>
> Jarek
> ====================================================\====
> 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
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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 mailing list