{"id":328091,"date":"2022-06-29T12:44:00","date_gmt":"2022-06-29T18:44:00","guid":{"rendered":"https:\/\/datasciencetut.com\/?p=448"},"modified":"2022-06-29T12:44:00","modified_gmt":"2022-06-29T18:44:00","slug":"how-to-find-unmatched-records-in-r-2","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2022\/06\/how-to-find-unmatched-records-in-r-2\/","title":{"rendered":"How to Find Unmatched Records in R"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"https:\/\/datasciencetut.com\/how-to-find-unmatched-records-in-r-2\/\"> Data Science Tutorials<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/datasciencetut.com\/how-to-find-unmatched-records-in-r-2\/\" target=\"_blank\">How to Find Unmatched Records in R<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/datasciencetut.com\/\" target=\"_blank\">Data Science Tutorials<\/a><\/p>\n\n<p>How to Find Unmatched Records in R?, To retrieve all rows in one data frame that do not have matching values in another data frame, use R\u2019s anti_join() function from the dplyr package.<\/p>\n\n\n\n<p>The basic syntax used by this function is as follows.<\/p>\n\n\n\n<p><a href=\"https:\/\/datasciencetut.com\/remove-columns-from-a-data-frame\/\" rel=\"nofollow\" target=\"_blank\">How to Remove Columns from a data frame in R \u2013 Data Science Tutorials<\/a><\/p>\n\n\n\n<pre>anti_join(df1, df2, by='col_name')<\/pre>\n\n\n\n<p>The usage of this syntax is demonstrated in the examples that follow.<\/p>\n\n\n\n<h2>Example 1: Use anti_join() with One Column<\/h2>\n\n\n\n<p>Suppose we have the two R data frames shown below:<\/p>\n\n\n\n<p>Let\u2019s build data frames<\/p>\n\n\n\n<pre>df1 &lt;- data.frame(Q1 = c('a', 'b', 'c', 'd', 'e', 'f'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Q2 = c(152, 514, 114, 218, 322, 323))\r\ndf2 &lt;- data.frame(Q1 = c('a', 'a', 'a', 'b', 'b', 'b'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Q3 = c(523, 324, 233, 134, 237, 141))<\/pre>\n\n\n\n<p>To return all rows in the first data frame that don\u2019t have a matching Q1 in the second data frame, we can use the anti_join() function.<\/p>\n\n\n\n<p><a href=\"https:\/\/datasciencetut.com\/bind-together-two-data-frames-by-their-rows-or-columns-in-r\/\" rel=\"nofollow\" target=\"_blank\">Bind together two data frames by their rows or columns in R (datasciencetut.com)<\/a><\/p>\n\n\n\n<pre>library(dplyr)<\/pre>\n\n\n\n<p>use the \u2018Q1\u2019 column to perform anti join<\/p>\n\n\n\n<pre>anti_join(df1, df2, by='Q1')\r\n\u00a0 Q1\u00a0 Q2\n1\u00a0 c 114\n2\u00a0 d 218\n3\u00a0 e 322\n4\u00a0 f 323<\/pre>\n\n\n\n<p>We can see that there are exactly 4 Q1\u2019s from the first data frame that does not have a matching Q1 name in the second data frame.<\/p>\n\n\n\n<h2>Example 2: Use anti_join() with Multiple Columns<\/h2>\n\n\n\n<p>Suppose we have the two R data frames shown below.<\/p>\n\n\n\n<p><a href=\"https:\/\/datasciencetut.com\/how-to-join-data-frames-for-different-column-names-in-r\/\" rel=\"nofollow\" target=\"_blank\">How to Join Data Frames for different column names in R (datasciencetut.com)<\/a><\/p>\n\n\n\n<p>Let\u2019s create a data frames<\/p>\n\n\n\n<pre>df1 &lt;- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 position=c('G', 'G', 'F', 'G', 'F', 'C'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 points=c(152, 114, 219, 254, 356, 441))\r\ndf2 &lt;- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 position=c('G', 'G', 'C', 'G', 'F', 'F'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 points=c(142, 214, 319, 133, 517, 422))<\/pre>\n\n\n\n<p>All rows in the first data frame that lack a matching team and position in the second data frame can be returned using the anti_join() function:<\/p>\n\n\n\n<pre>library(dplyr)<\/pre>\n\n\n\n<p>utilizing the columns for \u201cteam\u201d and \u201cposition,\u201d perform anti _join.<\/p>\n\n\n\n<p><a href=\"https:\/\/datasciencetut.com\/how-to-count-distinct-values-in-r\/\" rel=\"nofollow\" target=\"_blank\">How to Count Distinct Values in R \u2013 Data Science Tutorials<\/a><\/p>\n\n\n\n<pre>anti_join(df1, df2, by=c('team', 'position'))\r\n   team position points\n1\u00a0\u00a0\u00a0 A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 F\u00a0\u00a0\u00a0 219\n2\u00a0\u00a0\u00a0 B\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 C\u00a0\u00a0\u00a0 441<\/pre>\n\n\n\n<p>We can see that there are exactly two records from the first data frame that do not have a matching team name and position in the second data frame.<\/p>\n\n\n  \n  \n  <div class=\"\n    mailpoet_form_popup_overlay\n      \"><\/div>\n  <div\n    id=\"mailpoet_form_1\"\n    class=\"\n      mailpoet_form\n      mailpoet_form_html\n      mailpoet_form_position_\n      mailpoet_form_animation_\n    \"\n      >\n    \n    <style type=\"text\/css\">\n     .mailpoet_hp_email_label{display:none!important;}#mailpoet_form_1 .mailpoet_form {  }\n#mailpoet_form_1 form { margin-bottom: 0; }\n#mailpoet_form_1 h1.mailpoet-heading { margin: 0 0 20px; }\n#mailpoet_form_1 p.mailpoet_form_paragraph.last { margin-bottom: 5px; }\n#mailpoet_form_1 .mailpoet_column_with_background { padding: 10px; }\n#mailpoet_form_1 .mailpoet_form_column:not(:first-child) { margin-left: 20px; }\n#mailpoet_form_1 .mailpoet_paragraph { line-height: 20px; margin-bottom: 20px; }\n#mailpoet_form_1 .mailpoet_segment_label, #mailpoet_form_1 .mailpoet_text_label, #mailpoet_form_1 .mailpoet_textarea_label, #mailpoet_form_1 .mailpoet_select_label, #mailpoet_form_1 .mailpoet_radio_label, #mailpoet_form_1 .mailpoet_checkbox_label, #mailpoet_form_1 .mailpoet_list_label, #mailpoet_form_1 .mailpoet_date_label { display: block; font-weight: normal; }\n#mailpoet_form_1 .mailpoet_text, #mailpoet_form_1 .mailpoet_textarea, #mailpoet_form_1 .mailpoet_select, #mailpoet_form_1 .mailpoet_date_month, #mailpoet_form_1 .mailpoet_date_day, #mailpoet_form_1 .mailpoet_date_year, #mailpoet_form_1 .mailpoet_date { display: block; }\n#mailpoet_form_1 .mailpoet_text, #mailpoet_form_1 .mailpoet_textarea { width: 200px; }\n#mailpoet_form_1 .mailpoet_checkbox {  }\n#mailpoet_form_1 .mailpoet_submit {  }\n#mailpoet_form_1 .mailpoet_divider {  }\n#mailpoet_form_1 .mailpoet_message {  }\n#mailpoet_form_1 .mailpoet_form_loading { width: 30px; text-align: center; line-height: normal; }\n#mailpoet_form_1 .mailpoet_form_loading > span { width: 5px; height: 5px; background-color: #5b5b5b; }#mailpoet_form_1{border-radius: 16px;background: #ffffff;color: #313131;text-align: left;}#mailpoet_form_1 form.mailpoet_form {padding: 16px;}#mailpoet_form_1{width: 100%;}#mailpoet_form_1 .mailpoet_message {margin: 0; padding: 0 20px;}\n        #mailpoet_form_1 .mailpoet_validate_success {color: #00d084}\n        #mailpoet_form_1 input.parsley-success {color: #00d084}\n        #mailpoet_form_1 select.parsley-success {color: #00d084}\n        #mailpoet_form_1 textarea.parsley-success {color: #00d084}\n      \n        #mailpoet_form_1 .mailpoet_validate_error {color: #cf2e2e}\n        #mailpoet_form_1 input.parsley-error {color: #cf2e2e}\n        #mailpoet_form_1 select.parsley-error {color: #cf2e2e}\n        #mailpoet_form_1 textarea.textarea.parsley-error {color: #cf2e2e}\n        #mailpoet_form_1 .parsley-errors-list {color: #cf2e2e}\n        #mailpoet_form_1 .parsley-required {color: #cf2e2e}\n        #mailpoet_form_1 .parsley-custom-error-message {color: #cf2e2e}\n      #mailpoet_form_1 .mailpoet_paragraph.last {margin-bottom: 0} @media (max-width: 500px) {#mailpoet_form_1 {background: #ffffff;}} @media (min-width: 500px) {#mailpoet_form_1 .last .mailpoet_paragraph:last-child {margin-bottom: 0}}  @media (max-width: 500px) {#mailpoet_form_1 .mailpoet_form_column:last-child .mailpoet_paragraph:last-child {margin-bottom: 0}} \n    <\/style>\n\n    <form action=\"https:\/\/r-bloggers.com\/phplist\/?p=subscribe&#038;id=1\" method=\"post\" target=\"popupwindow\">\n      <input type=\"hidden\" name=\"data[form_id]\" value=\"1\" \/>\n      <input type=\"hidden\" name=\"token\" value=\"c404e87fa4\" \/>\n      <input type=\"hidden\" name=\"api_version\" value=\"v1\" \/>\n      <input type=\"hidden\" name=\"endpoint\" value=\"subscribers\" \/>\n      <input type=\"hidden\" name=\"mailpoet_method\" value=\"subscribe\" \/>\n\n      <label class=\"mailpoet_hp_email_label\">Please leave this field empty<input type=\"email\" name=\"data[email]\"\/><\/label><div class=\"mailpoet_paragraph \"><style>input[name=\"data[form_field_MGI0Nzk2NWMxZTIzX2VtYWls]\"]::placeholder{color:#abb8c3;opacity: 1;}<\/style><label for=\"form_email_1\" class=\"mailpoet-screen-reader-text\" style=\"font-size: 20px;line-height: 24px\";font-weight: bold;\"data-automation-id=\"form_email_label\" >Email Address <span class=\"mailpoet_required\">*<\/span><\/label><input type=\"email\" autocomplete=\"email\" class=\"mailpoet_text\" id=\"form_email_1\" name=\"data[form_field_MGI0Nzk2NWMxZTIzX2VtYWls]\" title=\"Email Address\" value=\"\" style=\"width:100%;background-color:#eeeeee;border-style:solid;border-radius:8px !important;border-width:0px;border-color:#313131;padding:16px;margin: 0 auto 0 0;font-family:&#039;Montserrat&#039;;font-size:20px;line-height:1.5;height:auto;color:#abb8c3;\" data-automation-id=\"form_email\"  placeholder=\"Email Address *\" data-parsley-required=\"true\" data-parsley-minlength=\"6\" data-parsley-maxlength=\"150\" data-parsley-error-message=\"Please specify a valid email address.\" data-parsley-required-message=\"This field is required.\"\/><\/div>\n<div class=\"mailpoet_paragraph \"><input type=\"submit\" class=\"mailpoet_submit\" value=\"Subscribe Now\" data-automation-id=\"subscribe-submit-button\" data-font-family='Montserrat' style=\"width:100%;background-color:#000000;border-style:solid;border-radius:8px !important;border-width:0px;padding:16px;margin: 0 auto 0 0;font-family:&#039;Montserrat&#039;;font-size:24px;line-height:1.5;height:auto;color:#ffd456;border-color:transparent;font-weight:bold;\" \/><span class=\"mailpoet_form_loading\"><span class=\"mailpoet_bounce1\"><\/span><span class=\"mailpoet_bounce2\"><\/span><span class=\"mailpoet_bounce3\"><\/span><\/span><\/div>\n\n      <div class=\"mailpoet_message\">\n        <p class=\"mailpoet_validate_success\"\n                style=\"display:none;\"\n                >Check your inbox or spam folder to confirm your subscription.\n        <\/p>\n        <p class=\"mailpoet_validate_error\"\n                style=\"display:none;\"\n                >        <\/p>\n      <\/div>\n    <\/form>\n  <\/div>\n\n  <p>The post <a rel=\"nofollow\" href=\"https:\/\/datasciencetut.com\/how-to-find-unmatched-records-in-r-2\/\" target=\"_blank\">How to Find Unmatched Records in R<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/datasciencetut.com\/\" target=\"_blank\">Data Science Tutorials<\/a><\/p>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"https:\/\/datasciencetut.com\/how-to-find-unmatched-records-in-r-2\/\"> Data Science Tutorials<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The post How to Find Unmatched Records in R appeared first on Data Science Tutorials<br \/>\nHow to Find Unmatched Records in R?, To retrieve all rows in one data frame that do not have matching values in another data frame, use R\u2019s anti_join() function from the dplyr package. &#8230;<\/p>\n","protected":false},"author":61,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/328091"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=328091"}],"version-history":[{"count":4,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/328091\/revisions"}],"predecessor-version":[{"id":328444,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/328091\/revisions\/328444"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=328091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=328091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=328091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}