repeating vector of letters

前端 未结 6 1313
野的像风
野的像风 2020-12-17 17:59

Is there a function to create a repeating list of letters in R?

something like

letters[1:30]
[1] \"a\" \"b\" \"c\" \"d\" \"e\" \"f\" \"g\" \"h\" \"i         


        
6条回答
  •  天命终不由人
    2020-12-17 18:49

    If you just want unique names, you could use

    make.unique(rep(letters, length.out = 30), sep='')
    

    Edit:

    Here's another way to get repeating letters using Reduce.

    myletters <- function(n) 
    unlist(Reduce(paste0, 
           replicate(n %/% length(letters), letters, simplify=FALSE),
           init=letters,
           accumulate=TRUE))[1:n]
    
    myletters(60)
    #  [1] "a"   "b"   "c"   "d"   "e"   "f"   "g"   "h"   "i"   "j"   "k"   "l"  
    # [13] "m"   "n"   "o"   "p"   "q"   "r"   "s"   "t"   "u"   "v"   "w"   "x"  
    # [25] "y"   "z"   "aa"  "bb"  "cc"  "dd"  "ee"  "ff"  "gg"  "hh"  "ii"  "jj" 
    # [37] "kk"  "ll"  "mm"  "nn"  "oo"  "pp"  "qq"  "rr"  "ss"  "tt"  "uu"  "vv" 
    # [49] "ww"  "xx"  "yy"  "zz"  "aaa" "bbb" "ccc" "ffffd" "eee" "fff" "ggg" "hhh"
    

提交回复
热议问题