Sequence of Alphabetical Character Letters from A-Z in R
In this article, we are going to discuss how to get a sequence of character letters from A-Z in R programming language.
Return all lower case letters using letters function
We will get all letters in lowercase sequence by using the letters function
Syntax:
letters
Example: Return all lowercase letters using letters function
R
# return all lower case letters in sequence print ( letters ) |
Output:
[1] “a” “b” “c” “d” “e” “f” “g” “h” “i” “j” “k” “l” “m” “n” “o” “p” “q” “r” “s”
[20] “t” “u” “v” “w” “x” “y” “z”
Return all upper case letters using LETTERS function
We will get all letters in uppercase sequence by using LETTERS function.
Syntax:
LETTERS
Example: Return all uppercase letters using LETTERS function
R
# return all upper case letters in sequence print ( LETTERS ) |
Output:
[1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J” “K” “L” “M” “N” “O” “P” “Q” “R” “S”
[20] “T” “U” “V” “W” “X” “Y” “Z”
Subsequence letters using range operation
We can get the subsequence of letters using the index. Index starts with 1 and ends with 26 (since there are 26 letters from a to z). We are getting from letters/LETTERS function.
Syntax:
letters[start:end]
LETTERS[start:end]
Where, start is the starting letter index and end is the ending letter index.
Example: R program to get subsequence letters using index
R
# return all upper case letters from # index 1 to index 12 print ( LETTERS [1:12]) # return all lower case letters from # index 1 to index 12 print ( letters [1:12]) # return all upper case letters from # index 5 to index 26 print ( LETTERS [5:26]) # return all lower case letters from # index 5 to index 26 print ( letters [5:26]) |
Output:
[1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J” “K” “L”
[1] “a” “b” “c” “d” “e” “f” “g” “h” “i” “j” “k” “l”
[1] “E” “F” “G” “H” “I” “J” “K” “L” “M” “N” “O” “P” “Q” “R” “S” “T” “U” “V” “W” “X” “Y” “Z”
[1] “e” “f” “g” “h” “i” “j” “k” “l” “m” “n” “o” “p” “q” “r” “s” “t” “u” “v” “w” “x” “y” “z”
Random sequence of letters using sample() function
Here in this scenario, we will get the random letters randomly using sample() function. sample() function is used to generate random letters
Syntax:
sample(letters/LETTERS,size)
Where,
- letters/LETTERS is a function that is a first parameter to display letters in lower/upper case
- size is used to get the number of letters randomly
Example: R program to display random letters
R
# display 20 random lower case letters sample ( letters , 20) # display 20 random upper case letters sample ( LETTERS , 20) # display 17 random lower case letters sample ( letters , 17) # display 17 random upper case letters sample ( LETTERS , 17) |
Output:
[1] “k” “p” “g” “c” “j” “r” “s” “u” “h” “i” “d” “o” “a” “m” “y” “f” “t” “l” “q” “b”
[1] “D” “A” “K” “G” “W” “E” “N” “C” “P” “T” “M” “S” “F” “V” “B” “R” “H” “Y” “X” “I”
[1] “i” “v” “f” “u” “s” “d” “x” “w” “h” “a” “p” “b” “y” “o” “c” “z” “l”
[1] “Y” “X” “Z” “C” “N” “M” “A” “Q” “V” “R” “O” “E” “B” “J” “F” “U” “I”
Please Login to comment...