w3resource

R Programming: Get the unique elements of a given string and unique numbers of vector

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 :

str1 = "The quick brown fox jumps over the lazy dog."
print("Original vector(string)")
print(str1)
print("Unique elements of the said vector:")
print(unique(tolower(str1)))
nums = c(1, 2, 2, 3, 4, 4, 5, 6)
print("Original vector(number)")
print(nums)
print("Unique elements of the said vector:")
print(unique(nums))

Sample 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                         

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 Python skills with w3resource's quiz

What is the difficulty level of this exercise?