w3resource

Extract first 10 lowercase, last 10 uppercase, and 22nd-24th uppercase letters in R

R Programming: Basic Exercise-8 with Solution

Write a R program to extract first 10 english letter in lower case and last 10 letters in upper case and extract letters between 22nd to 24th letters in upper case.

Note: Use built-in datasets letters and LETTERS.

Sample Solution :

R Programming Code :

# Print a message indicating the first 10 letters in lower case
print("First 10 letters in lower case:")

# Get the first 10 letters of the alphabet in lower case
t = head(letters, 10)

# Print the first 10 letters in lower case
print(t)

# Print a message indicating the last 10 letters in upper case
print("Last 10 letters in upper case:")

# Get the last 10 letters of the alphabet in upper case
t = tail(LETTERS, 10)

# Print the last 10 letters in upper case
print(t)

# Print a message indicating the letters between the 22nd and 24th in upper case
print("Letters between 22nd to 24th letters in upper case:")

# Get the letters between the 22nd and 24th positions in upper case
e = tail(LETTERS[22:24])

# Print the letters between the 22nd and 24th positions in upper case
print(e)

Output:

[1] "First 10 letters in lower case:"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
[1] "Last 10 letters in upper case:"
 [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
[1] "Letters between 22nd to 24th letters in upper case:"
[1] "V" "W" "X"                         

Explanation:

  • print("First 10 letters in lower case:")
    Displays a message indicating the upcoming output.
  • t = head(letters, 10)
    Extracts the first 10 letters from the letters dataset (which contains lowercase English letters).
  • print(t)
    Prints the first 10 lowercase letters.
  • print("Last 10 letters in upper case:")
    Displays a message indicating the upcoming output.
  • t = tail(LETTERS, 10)
    Extracts the last 10 letters from the LETTERS dataset (which contains uppercase English letters).
  • print(t)
    Prints the last 10 uppercase letters.
  • print("Letters between 22nd to 24th letters in upper case:")
    Displays a message indicating the upcoming output.
  • e = tail(LETTERS[22:24])
    Extracts the 22nd to 24th uppercase letters from the LETTERS dataset and stores them in e.
  • print(e)
    Prints the letters between the 22nd and 24th positions in uppercase.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to print the numbers from 1 to 100 and print "Fizz" for multiples of 3, print "Buzz" for multiples of 5, and print "FizzBuzz" for multiples of both.
Next: Write a R program to find the factors of a given number.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-8.php