On Aug 8, 2010, at 9:50 AM, david h shanabrook wrote:

> How can I get a substring based on the index into the string?
> 
> strM <- c("abcde", "cdefg")
> ind <- c(1,3,5)
> 
> I want to use ind to index into the strings so the result is:
> 
> strMind <- c("ace", "ceg")



Here is one way:

> apply(sapply(ind, function(i) substr(strM, i, i)), 
        1, paste, collapse = "")
[1] "ace" "ceg"


and another:

> sapply(strsplit(strM, ""), 
         function(x) paste(x[ind], collapse = ""))
[1] "ace" "ceg"


A test using a replication of strM to create a much larger source vector 
suggests that the second method is meaningfully faster than the second.


Yet another option would be:

> gsub("^(.).(.).(.)$", "\\1\\2\\3", strM)
[1] "ace" "ceg"


and this is the fastest of the three on a large source vector by a factor of 
~10 over the second approach above.  However, it is less easily generalizable.

You might also want to look at Gabor's 'gsubfn' CRAN package to see if he is 
utilities there that may be relevant.

HTH,

Marc Schwartz

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to