mikeblazanin / gcplyr

gcplyr is an R package that facilitates wrangling and analysis of microbial growth curve data
https://mikeblazanin.github.io/gcplyr/
Other
30 stars 2 forks source link

Utility Function: seq.character #133

Open discoleo opened 1 year ago

discoleo commented 1 year ago

New Utility Function: seq.character

The generation of sequences of letters is currently hard-coded in many of the functions. However, it may be useful to be able to generate sequences of letters on the fly. A function seq.character may be useful in such situations.

R Code

Usage

seq("A", "H")
# the 2nd argument can act both as "to" or "length.out";
seq("A", 8)
# covers also: c("Str1", ..., "Str6");
seq("Str", 6)

Note:

discoleo commented 1 year ago

Enhanced Code

The seq.character function is now overloaded:

R Code

seq.character = function(from, to) {
    if(nchar(from[1]) > 1) {
        # convenience functionality;
        if(is.numeric(to)) return(seq.nchar(from, to));
    }
    from = as.integer(charToRaw(from));
    isUpper = if(from <= 90) TRUE else FALSE;
    off = if(isUpper) 64 else 96;
    from = from - off;
    # Length:
    if(is.numeric(to)) {
        if(isUpper) return(LETTERS[seq(from, from + to - 1)]);
        return(letters[seq(from, from + to)]);
    }
    to = as.integer(charToRaw(to)) - off;
    if(isUpper) return(LETTERS[seq(from, to)]);
    return(letters[seq(from, to)]);
}
# simple replacement for paste(from, seq(n), sep="")
seq.nchar = function(from, length.out, start.at=1, sep="") {
    paste(from, seq(start.at, length.out=length.out), sep=sep)
}

Examples

# A:H seq("A", "H") # A, length = 8 seq("A", 8) # c("Str1", ..."Str6") seq("Str", 6)