[R] Q. About String indexing

John Fox jfox at mcmaster.ca
Fri Nov 7 03:24:02 CET 2003


Dear Ben and Mark,

substring and substr are also helpful here.

John

At 08:10 PM 11/6/2003 -0600, Marc Schwartz wrote:
>On Thu, 2003-11-06 at 19:53, Ben Day wrote:
> > Hi all - this is a very basic question: how does one index substrings
> > of a character string, for example get the 2nd through 5th characters
> > of the string "testing", in the R language? Indexing with square
> > brackets seems only to work for vectors, lists, etc., and although
> > I've found plenty of functions for concatenating strings, running
> > substitutions with regexps and the like, I seem to be at a loss when
> > it comes to indexing or selecting substrings.
> >
> > I'm sure I'm missing something very obvious here, but I ask only after
> > having scoured the basic help manuals.
> >
> > I am not so subscribed to this list, so if any replies could be sent
> > directly to me.
> >
> > Thanks very much,
> > -----Ben
>
>
>unlist(strsplit("testing",""))[2:5]
>[1] "e" "s" "t" "i"
>
>Use strsplit() to convert the string into a list of characters, where ""
>is the separator.
>
>Then use unlist() to convert the list into a vector and index the 2:5
>characters.
>
>See ?strsplit and ?unlist
>
>HTH,
>
>Marc Schwartz
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox




More information about the R-help mailing list