Slip
3
Write an R program to compare two data frames to find the elements in
first data frame that are not present in second data frame.
df_1 = data.frame(
"item" =
c("item1", "item2", "item3"),
"A" = c(1, 4, 12),
"B" = c(2, 5, 15),
"C" = c(3, 6, 15)
)
df_2 = data.frame(
"item" =
c("item1", "item2", "item3"),
"A" = c(1, 4, 12),
"B" = c(2, 5, 15),
"C" = c(3, 7, 18)
)
print("Original Dataframes:")
print(df_1)
print(df_2)
print("Row(s) in first data frame that are not present in second
data frame:")
print(setdiff(df_1,df_2))
OUTPUT
> print("Original Dataframes:")
[1] "Original Dataframes:"
> print(df_1)
item A
B C
1 item1 1 2 3
2 item2 4 5 6
3 item3 12 15 15
> print(df_2)
item A
B C
1 item1 1 2 3
2 item2 4 5 7
3 item3 12 15 18
> print("Row(s) in first data frame that are not present in
second data frame:")
[1] "Row(s) in first data frame that are not present in second data
frame:"
> print(setdiff(df_1,df_2))
$C
[1] 3 6 15