[R] Q. About String indexing
Marc Schwartz
MSchwartz at medanalytics.com
Fri Nov 7 03:10:34 CET 2003
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
More information about the R-help
mailing list