w3resource

R Programming: Create Data Frame and Display Duplicates & Unique rows

R Programming: Data frame Exercise-24 with Solution

Write a R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame.

Sample Solution :

R Programming Code :

# Create vector 'a' with specified values
a = c(10, 20, 10, 10, 40, 50, 20, 30)

# Create vector 'b' with specified values
b = c(10, 30, 10, 20, 0, 50, 30, 30)

# Print a message indicating that the original data frame will be displayed
print("Original data frame:")

# Combine vectors 'a' and 'b' into a data frame 'ab'
ab = data.frame(a, b)

# Print the data frame 'ab'
print(ab)

# Print a message indicating that duplicated elements in the data frame will be shown
print("Duplicate elements of the said data frame:")

# Display duplicated elements of the data frame 'ab'
print(duplicated(ab))

# Print a message indicating that unique rows of the data frame will be shown
print("Unique rows of the said data frame:")

# Display unique rows of the data frame 'ab'
print(unique(ab))

Output:

[1] "Original data frame:"
   a  b
1 10 10
2 20 30
3 10 10
4 10 20
5 40  0
6 50 50
7 20 30
8 30 30
[1] "Duplicate elements of the said data frame:"
[1] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
[1] "Unique rows of the said data frame:"
   a  b
1 10 10
2 20 30
4 10 20
5 40  0
6 50 50
8 30 30                         

Explanation:

  • Define Vector a:
    • a = c(10, 20, 10, 10, 40, 50, 20, 30)
    • Creates a vector a with the specified numeric values.
  • Define Vector b:
    • b = c(10, 30, 10, 20, 0, 50, 30, 30)
    • Creates a vector b with the specified numeric values.
  • Print Message:
    • print("Original data frame:")
    • Prints the message indicating that the original data frame will be shown.
  • Create Data Frame:
    • ab = data.frame(a, b)
    • Combines vectors a and b into a data frame named ab.
  • Print Data Frame:
    • print(ab)
    • Displays the contents of the data frame ab.
  • Print Message for Duplicates:
    • print("Duplicate elements of the said data frame:")
    • Prints the message indicating that duplicated elements in the data frame will be shown.
  • Print Duplicated Elements:
    • print(duplicated(ab))
    • Displays a logical vector indicating which rows in the data frame ab are duplicates.
  • Print Message for Unique Rows:
    • print("Unique rows of the said data frame:")
    • Prints the message indicating that unique rows in the data frame will be shown.
  • Print Unique Rows:
    • print(unique(ab))
    • Displays the unique rows of the data frame ab, removing any duplicate rows.

R Programming Code Editor:



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

Previous: Write a R program to count the number of NA values in a data frame column.
Next: Write a R program to call the (built-in) dataset airquality. Check whether it is a data.frame or not? Order the entire data frame by the first and second column.

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://w3resource.com/r-programming-exercises/dataframe/r-programming-data-frame-exercise-24.php