repeating vector of letters

前端 未结 6 1335
野的像风
野的像风 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:34

    Probably not the cleanest, but easy to see what's happening:

    foo<-letters[1:26]
    outlen <- 73 # or whatever length you want
     oof <- vector(len=26)
    for ( j in 2:(outlen%/%26)) {
        for (k in 1:26) oof[k] <- paste(rep(letters[k],j),sep='',collapse='')
        foo<-c(foo,oof)
    }
    for (jj in 1:(outlen%%26) ) foo[(26*j)+jj]<-paste(rep(letters[jj],(j+1)),sep='',collapse='')
    
    foo
    [1] "a"   "b"   "c"   "d"   "e"   "f"   "g"   "h"   "i"   "j"   "k"   "l"   "m"   "n"  
    [15] "o"   "p"   "q"   "r"   "s"   "t"   "u"   "v"   "w"   "x"   "y"   "z"   "aa"  "bb" 
    [29] "cc"  "dd"  "ee"  "ff"  "gg"  "hh"  "ii"  "jj"  "kk"  "ll"  "mm"  "nn"  "oo"  "pp" 
    [43] "qq"  "rr"  "ss"  "tt"  "uu"  "vv"  "ww"  "xx"  "yy"  "zz"  "aaa" "bbb" "ccc" "ffffd"
    [57] "eee" "fff" "ggg" "hhh" "iii" "jjj" "kkk" "lll" "mmm" "nnn" "ooo" "ppp" "qqq" "rrr"
    [71] "sss" "ttt" "uuu"
    

    EDIT: Matthew wins, hands-down:

    microbenchmark(anandaLetters(5000),matthewletters(5000),carlletters(5000),times=10)
    Unit: milliseconds
                     expr       min        lq     median        uq        max neval
      anandaLetters(5000) 85.339200 85.567978 85.9827715 86.260298  86.612231    10
     matthewletters(5000)  3.413706  3.503506  3.9067535  3.946950   4.106453    10
        carlletters(5000) 94.893983 95.405418 96.4492430 97.234784 110.681780    10
    

提交回复
热议问题