在R中,如何从A到Z打印字符列表?用整数我可以说:
my_list = c(1:10)
> my_list
[1] 1 2 3 4 5 6 7 8 9 10
但是我可以对角色做同样的事情吗?例如
my_char_list = c(A:Z)
my_char_list = c("A":"Z")
这些不起作用,我希望输出为:"A" "B" "C" "D"
,或用逗号分隔。
答案 0 :(得分:17)
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"
答案 1 :(得分:12)
> 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"
>
答案 2 :(得分:0)
strsplit(intToUtf8(c(97:122)),"")
对于a,b,c,...,z
strsplit(intToUtf8(c(65:90)),"")
对于A,B,C,...,Z
答案 3 :(得分:0)
#' 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]
}