[Rd] suggestion for addition to substring

Patrick Burns pburns at pburns.seanet.com
Sun Jun 27 11:55:30 CEST 2004


Below is suggested code for adding a "tail" argument to the
"substring" function.

Getting the end of a string is a fairly common operation in
finance where the exchange of an asset is often encoded.

This is a new implementation,  but I don't think there are any
tricky bits I haven't thought of.

function (text, first, last = 1e+06, tail=NULL)
{
    storage.mode(text) <- "character"
    if(length(tail)) {
        if(!missing(first))
                stop("only one of first and tail allowed")
        nc <- nchar(text)
        first <- nc - tail + 1
    }
    n <- max(lt <- length(text), length(first), length(last))
    if (lt && lt < n)
        text <- rep(text, length.out = n)
    substr(text, first, last)
}
<environment: namespace:base>

As implemented here "tail" gives the distance from the original end of the
strings, not the number of characters in the answer (when "last" is given).


Patrick Burns

Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")



More information about the R-devel mailing list