Find elements that appear only once and are common to both Data Frames in R
Write a R program to find elements come only once that are common to both given data frames.
Sample Solution:
R Programming Code:
# Create the first vector 'a' with elements "a", "b", "c", "d", "e"
a = c("a", "b", "c", "d", "e")
# Create the second vector 'b' with elements "d", "e", "f", "g"
b = c("d", "e", "f", "g")
# Print message indicating the original dataframes (vectors in this context)
print("Original Dataframes")
# Print the contents of the first vector 'a'
print(a)
# Print the contents of the second vector 'b'
print(b)
# Print message indicating the operation to find elements appearing only once in both vectors
print("Find elements come only once that are common to both given dataframes:")
# Find the union of both vectors, which combines all unique elements from both
result = union(a, b)
# Print the result of the union operation
print(result)
Output:
[1] "Original Dataframes" [1] "a" "b" "c" "d" "e" [1] "d" "e" "f" "g" [1] "Find elements come only once that are common to both given dataframes:" [1] "a" "b" "c" "d" "e" "f" "g"
Explanation:
Here is a brief explanation of the given R Programming code:
- Create vector a:
- The vector a is created with elements ("a", "b", "c", "d", "e").
- Create vector b:
- The vector b is created with elements ("d", "e", "f", "g").
- Print the label for original dataframes:
- Prints the message "Original Dataframes" to indicate that the original vectors will be displayed.
- Print vector a:
- Displays the contents of vector a: ("a", "b", "c", "d", "e").
- Print vector b:
- Displays the contents of vector b: ("d", "e", "f", "g").
- Print the label for operation:
- Prints the message "Find elements come only once that are common to both given dataframes:" to indicate the operation being performed.
- Find the union of vectors a and b:
- The union(a, b) function combines all unique elements from both vectors a and b. The result is stored in the variable result.
- Print the result:
- Displays the result of the union operation: ("a", "b", "c", "d", "e", "f", "g"), which contains all unique elements present in either vector.
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 elements which are present in two given data frames.
Next: Write a R program to save the information of a data frame in a file and display the information of the file.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics