Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeating vector of letters

Tags:

r

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" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z" NA  NA  NA  NA

but instead of NA, I would like the output to continue aa, bb, cc, dd ...

like image 829
John Waller Avatar asked Feb 10 '14 15:02

John Waller


1 Answers

It's not too difficult to piece together a quick function to do something like this:

myLetters <- function(length.out) {
  a <- rep(letters, length.out = length.out)
  grp <- cumsum(a == "a")
  vapply(seq_along(a), 
         function(x) paste(rep(a[x], grp[x]), collapse = ""),
         character(1L))
}
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" "ddd" "eee" "fff" "ggg" "hhh"
like image 186
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 19 '22 02:10

A5C1D2H2I1M1N2O1R2T1