From pburns at pburns.seanet.com  Sun Jun 27 11:55:30 2004
From: pburns at pburns.seanet.com (Patrick Burns)
Date: Sun Jun 27 11:55:46 2004
Subject: [Rd] suggestion for addition to substring
Message-ID: <40DE9992.7070701@pburns.seanet.com>

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@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")


[Rd] suggestion for addition to substring
Patrick Burns