is there a way to extend LETTERS past 26 characters e.g., AA, AB, AC…?

前端 未结 9 1729
情书的邮戳
情书的邮戳 2020-12-03 00:52

I use LETTERS most of the time for my factors but today I tried to go beyond 26 characters:

LETTERS[1:32]

Expecting there to be an automati

9条回答
  •  孤城傲影
    2020-12-03 01:33

    This solution uses recursion. Usage is a bit different in the sense MORELETTERS is not a long vector you will have to store and possibly expand as your inputs get larger. Instead, it is a function that converts your numbers into the new base.

    extend <- function(alphabet) function(i) {
       base10toA <- function(n, A) {
          stopifnot(n >= 0L)
          N <- length(A)
          j <- n %/% N 
          if (j == 0L) A[n + 1L] else paste0(Recall(j - 1L, A), A[n %% N + 1L])
       }   
       vapply(i-1L, base10toA, character(1L), alphabet)
    }
    MORELETTERS <- extend(LETTERS)         
    
    MORELETTERS(1:1000)
    # [1] "A" "B" ... "ALL"
    MORELETTERS(c(1, 26, 27, 1000, 1e6, .Machine$integer.max))
    # [1] "A"       "Z"       "AA"      "ALL"     "BDWGN"   "FXSHRXW"
    

提交回复
热议问题