R Program to find Unique elements in Strings and Vectors
R Programming: Basic Exercise-11 with Solution
Write a R program to get the unique elements of a given string and unique numbers of vector.
Sample Solution :
R Programming Code :
# Assign a string to the variable str1
str1 = "The quick brown fox jumps over the lazy dog."
# Print a message indicating that the following output is the original string
print("Original vector(string)")
# Print the content of the string
print(str1)
# Print a message indicating that the following output will show unique elements of the string
print("Unique elements of the said vector:")
# Convert the string to lowercase and print unique characters
print(unique(tolower(str1)))
# Create a vector of numbers with some duplicates
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
# Print a message indicating that the following output is the original number vector
print("Original vector(number)")
# Print the content of the number vector
print(nums)
# Print a message indicating that the following output will show unique elements of the number vector
print("Unique elements of the said vector:")
# Print unique numbers from the vector
print(unique(nums))
Output:
[1] "Original vector(string)" [1] "The quick brown fox jumps over the lazy dog." [1] "Unique elements of the said vector:" [1] "the quick brown fox jumps over the lazy dog." [1] "Original vector(number)" [1] 1 2 2 3 4 4 5 6 [1] "Unique elements of the said vector:" [1] 1 2 3 4 5 6
Explanation:
- str1 = "The quick brown fox jumps over the lazy dog.": Assigns a string to the variable str1.
- print("Original vector(string)"): Prints the label for the original string.
- print(str1): Displays the content of the string str1.
- print("Unique elements of the said vector:"): Prints a label indicating the unique elements in the string.
- print(unique(tolower(str1))): Converts all characters in str1 to lowercase and prints the unique elements from the string.
- nums = c(1, 2, 2, 3, 4, 4, 5, 6): Creates a vector nums containing a set of numeric values, some of which are duplicates.
- print("Original vector(number)"): Prints the label for the original numeric vector.
- print(nums): Displays the content of the numeric vector nums.
- print("Unique elements of the said vector:"): Prints a label indicating the unique elements in the numeric vector.
- print(unique(nums)): Extracts and prints the unique elements from the vector nums.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to find the maximum and the minimum value of a given vector.
Next: Write a R program to create three vectors a,b,s with 3 integers. Combine the three vectors to become a 3×3 matrix where each column represents a vector. Print the content of the matrix.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
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://w3resource.com/r-programming-exercises/basic/r-programming-basic-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics