开发者

How to print a character list from A to Z?

In R, ho开发者_运维技巧w can I print a character list from A to Z? With integers I can say:

my_list = c(1:10)
> my_list
 [1]  1  2  3  4  5  6  7  8  9 10

But can I do the same with characters? e.g.

my_char_list = c(A:Z)
my_char_list = c("A":"Z")

These don't work, I want the output to be: "A" "B" "C" "D", or separated by commas.


LETTERS

"A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"


> LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X"
[25] "Y" "Z"
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x"
[25] "y" "z"
> LETTERS[5:10]
[1] "E" "F" "G" "H" "I" "J"
> 


strsplit(intToUtf8(c(97:122)),"") 

for a,b,c,...,z

strsplit(intToUtf8(c(65:90)),"") 

for A,B,C,...,Z


#' range_ltrs() returns a vector of letters
#'
#' range_ltrs() returns a vector of letters,
#' starting with arg start and ending with arg stop.
#' Start and stop must be the same case.
#' If start is after stop, then a "backwards" vector is returned.
#'
#' @param start an upper or lowercase letter.
#' @param stop an upper or lowercase letter.
#'
#' @examples
#' > range_ltrs(start = 'A', stop = 'D')
#' [1] "A" "B" "C" "D"
#'
#' If start is after stop, then a "backwards" vector is returned.
#' > range_ltrs('d', 'a')
#' [1] "d" "c" "b" "a"
range_ltrs <- function (start, stop) {
  is_start_upper <- toupper(start) == start
  is_stop_upper <- toupper(stop) == stop
  if (is_start_upper) stopifnot(is_stop_upper)
  if (is_stop_upper) stopifnot(is_start_upper)
  ltrs <- if (is_start_upper) LETTERS else letters
  start_i <- which(ltrs == start)
  stop_i <- which(ltrs == stop)
  ltrs[start_i:stop_i]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜