w3resource

R Programming: Compare two data frames to find the row(s) in first data frame that are not present in second data frame

R Programming: Data frame Exercise-19 with Solution

Write a R program to compare two data frames to find the row(s) in first data frame that are not present in second data frame.

Sample Solution:

R Programming Code:

df_90 = data.frame(
  "item" = c("item1", "item2", "item3"),
  "Jan_sale" = c(12, 14, 12),
  "Feb_sale" = c(11, 12, 15),
  "Mar_sale" = c(12, 14, 15)
)
df_91 = data.frame(
  "item" = c("item1", "item2", "item3"),
  "Jan_sale" = c(12, 14, 12),
  "Feb_sale" = c(11, 12, 15),
  "Mar_sale" = c(12, 15, 18)
)
print("Original Dataframes:")
print(df_90)
print(df_91)
print("Row(s) in first data frame that are not present in second data frame:")
print(setdiff(df_90,df_91))

Sample Output:

[1] "Original Dataframes:"
   item Jan_sale Feb_sale Mar_sale
1 item1       12       11       12
2 item2       14       12       14
3 item3       12       15       15
   item Jan_sale Feb_sale Mar_sale
1 item1       12       11       12
2 item2       14       12       15
3 item3       12       15       18
[1] "Row(s) in first data frame that are not present in second data frame:"
  Mar_sale
1       12
2       14
3       15

R Programming Code Editor:



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

Previous: Write a R program to reorder an given data frame by column name.
Next: Write a R program to find elements which are present in two given data frames.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.