rcompare

compare specific column values between two dataframes in R


i have two data frames df1 and df2 where i want to compare values in a specific column present in both dataframes. How do i get the deisred output mention below in any format such a string or tibble or dataframe. Please note there will be duplicates in both data frames

df1 <- data.frame (channel= c("BNN", "BBC", "ABC" ,"SKY"),
                  second_column = c("value_1", "value_2", "value_3","value_4")
                  )

df2 <- data.frame (channel= c("CNN", "BBC", "ABC"),
                  second_column = c("value_1", "value_2", "value_3")
                  )

Desired output will be based on df2

Missing values in df2 : SKY,BNN

New values in df2 : CNN


Solution

  • You can use setdiff(x,y) to identify the values in x that are not present in y.

    df1 <- data.frame (channel= c("BNN", "BBC", "ABC" ,"SKY"),
                       second_column = c("value_1", "value_2", "value_3","value_4")
    )
    
    df2 <- data.frame (channel= c("CNN", "BBC", "ABC"),
                       second_column = c("value_1", "value_2", "value_3")
    )
    
    list(
    "Missing in df2" = setdiff(df1$channel, df2$channel), 
    "New in df2" = setdiff(df2$channel, df1$channel))
    #> $`Missing in df2`
    #> [1] "BNN" "SKY"
    #> 
    #> $`New in df2`
    #> [1] "CNN"
    

    Created on 2023-09-26 with reprex v2.0.2