从'A' - 'Z'生成一系列字符

Jam*_*son 31 ascii r character sequence seq

我可以制作一系列这样的数字:

s = seq(from=1, to=10, by=1)
Run Code Online (Sandbox Code Playgroud)

如何从AZ制作一系列字符?这不起作用:

seq(from=1, to=10)
Run Code Online (Sandbox Code Playgroud)

Sha*_*ane 46

使用LETTERSletters(分别为大写和小写).


Jos*_*ich 34

使用您拥有的代码letters和/或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"
Run Code Online (Sandbox Code Playgroud)

  • 或者只是字母[1:10] (2认同)

Dir*_*tel 15

只需使用预定义的变量lettersLETTERS.

为了完整性,这里有一些使用seq:

R> rawToChar(as.raw(seq(as.numeric(charToRaw('a')), as.numeric(charToRaw('z')))))
[1] "abcdefghijklmnopqrstuvwxyz"
R> 
Run Code Online (Sandbox Code Playgroud)


Spa*_*man 11

R.oo包有一个intToChar功能,即使用ASCII值,如果LETTERSletters是没有任何好处的.A是65的ASCII:

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

或者您可以使用以下事实:最低的unicode数字是ascii,因此intToUtf8在R-base中如下:

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

或者说rawToChar:

> rawToChar(as.raw(65:78))
[1] "ABCDEFGHIJKLMN"
Run Code Online (Sandbox Code Playgroud)