42

I can make a sequence of numbers like this:

s = seq(from=1, to=10, by=1)

How do I make a sequence of characters from A-Z? This doesn't work:

seq(from=1, to=10)
smci
  • 32,567
  • 20
  • 113
  • 146
James Thompson
  • 46,512
  • 18
  • 65
  • 82

5 Answers5

61

Use LETTERS and letters (for uppercase and lowercase respectively).

Shane
  • 98,550
  • 35
  • 224
  • 217
47

Use the code you have with letters and/or LETTERS:

> LETTERS[seq( from = 1, to = 10 )]
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
> letters[seq( from = 1, to = 10 )]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
16

Just use the predefined variables letters and LETTERS.

And for completeness, here it something using seq:

R> rawToChar(as.raw(seq(as.numeric(charToRaw('a')), as.numeric(charToRaw('z')))))
[1] "abcdefghijklmnopqrstuvwxyz"
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
12

R.oo package has an intToChar function, that uses ASCII values, if LETTERS and letters aren't any good. A is 65 in ASCII:

> require(R.oo)
> intToChar(65:79)
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"

or you can use the fact that the lowest unicode numbers are ascii and hence intToUtf8 in R-base like this:

> intToUtf8(65:78,multiple=TRUE)
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N"

or faff around with rawToChar:

> rawToChar(as.raw(65:78))
[1] "ABCDEFGHIJKLMN"
zx8754
  • 52,746
  • 12
  • 114
  • 209
Spacedman
  • 92,590
  • 12
  • 140
  • 224
2

LETTERS returns A-Z

To generate A-E for instance

Uppercase:

> LETTERS[1:5]

Lowercase

letters[1:5]
Wakeel
  • 4,272
  • 2
  • 13
  • 14