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

时间:2010-12-06 19:13:58

标签: r character ascii sequence seq

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

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

如何从A-Z制作一系列角色?这不起作用:

seq(from=1, to=10)

4 个答案:

答案 0 :(得分:42)

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

答案 1 :(得分: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"

答案 2 :(得分:14)

只需使用预定义变量lettersLETTERS

为了完整起见,请使用seq

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

答案 3 :(得分:11)

R.oo 包有一个intToChar函数,如果LETTERSletters没有任何好处,则使用ASCII值。 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"

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

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

rawToChar

> rawToChar(as.raw(65:78))
[1] "ABCDEFGHIJKLMN"