Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading