[R] Q. About String indexing

Marc Schwartz MSchwartz at medanalytics.com
Fri Nov 7 04:32:04 CET 2003


On Thu, 2003-11-06 at 20:24, John Fox wrote:
> Dear Ben and Mark,
> 
> substring and substr are also helpful here.
> 
> John


Quite true. 

As usual, there is usually more than one way to do things. The above
approach, given the C code, would be faster as the source vector and the
offsets of the substring increase in size.

This would become more influential if you want the resultant characters
as a single substring as opposed to a vector of the characters, which my
initial solution would yield.

> unlist(strsplit("testing", ""))[2:5]
[1] "e" "s" "t" "i"

> substr("testing", 2, 5)
[1] "esti"

Thanks John.

Marc




More information about the R-help mailing list