Write a R program to get the unique elements of a given string and unique numbers of vector

1. Introduction

This R program is designed to find unique elements in both a given string and a numeric vector. It extracts unique characters from the string and unique numbers from the vector.

2. Program Steps

1. Define a string and a numeric vector.

2. Extract unique characters from the string.

3. Extract unique numbers from the vector.

4. Display the unique characters and numbers.

3. Code Program

# Define a string
str1 <- "example string"
# Convert the string to a vector of characters
char_vector <- unlist(strsplit(str1, ""))
# Extract unique characters
unique_chars <- unique(char_vector)

# Define a numeric vector
num_vector <- c(1, 2, 2, 3, 4, 5, 5, 6)
# Extract unique numbers
unique_nums <- unique(num_vector)

# Display the results
print("Unique characters in the string:")
print("Unique numbers in the vector:")


Unique characters in the string:
[Unique characters from the string]
Unique numbers in the vector:
[Unique numbers from the vector]


1. str1 <- "example string": Initializes the string.

2. unlist(strsplit(str1, "")): Splits the string into individual characters and converts it to a vector.

3. unique(char_vector): Finds unique characters in the character vector.

4. num_vector <- c(1, 2, 2, 3, 4, 5, 5, 6): Initializes the numeric vector.

5. unique(num_vector): Finds unique numbers in the numeric vector.

6. print("Unique characters in the string:"): Prints a message indicating the unique characters in the string.

7. print(unique_chars): Displays the unique characters.

8. print("Unique numbers in the vector:"): Prints a message indicating the unique numbers in the vector.

9. print(unique_nums): Displays the unique numbers.