{"id":234189,"date":"2021-12-15T09:28:05","date_gmt":"2021-12-15T15:28:05","guid":{"rendered":"https:\/\/finnstats.com\/?p=14369"},"modified":"2021-12-15T09:28:05","modified_gmt":"2021-12-15T15:28:05","slug":"how-to-find-unique-values-in-r","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2021\/12\/how-to-find-unique-values-in-r\/","title":{"rendered":"How to Find Unique Values 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:\/\/finnstats.com\/index.php\/2021\/12\/15\/how-to-find-unique-values-in-r\/\"> Methods \u2013 finnstats<\/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>Visit for the most up-to-date information on Data Science, employment, and tutorials <a rel=\"nofollow\" href=\"https:\/\/finnstats.com\/\" target=\"_blank\">finnstats<\/a>.<\/p>\n<p>If you want to read the original article, go here <a rel=\"nofollow\" href=\"https:\/\/finnstats.com\/index.php\/2021\/12\/15\/how-to-find-unique-values-in-r\/\" target=\"_blank\">How to Find Unique Values in R<\/a><\/p>\n\n<p>To find unique values in a column in a data frame, use the unique() function in R.<\/p>\n\n\n\n<p>In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data. <\/p>\n\n\n\n<p>Let\u2019s look at how to get the unique data out of the R object.<\/p>\n\n\n\n<h2>Syntax<\/h2>\n\n\n\n<pre>unique(data)<\/pre>\n\n\n\n<p>data: It is a vector \/ data frame \/array \/ NULL.<\/p>\n\n\n\n<p>The following data frame is used in this tutorial to demonstrate how to utilize this function.<\/p>\n\n\n\n<p><a href=\"https:\/\/finnstats.com\/index.php\/2021\/05\/07\/pca\/\" rel=\"nofollow\" target=\"_blank\">Principal Component Analysis in R \u00bb finnstats<\/a><\/p>\n\n\n\n<p>Let\u2019s create a data frame for illustration purposes,<\/p>\n\n\n\n<pre>data &lt;- data.frame(Product=c('A', 'A', 'B', 'B', 'C', 'C'),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Likeability=c(80, 98, 80, 82, 70, 65),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Score=c(31, 30, 33, 33, 33, 23),\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Quality=c(16, 24, 32, 56, 18, 12))<\/pre>\n\n\n\n<p>Let\u2019s view the data frame<\/p>\n\n\n\n<pre>data\r\n    Product Likeability Score Quality\n1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 80\u00a0\u00a0\u00a0 31\u00a0\u00a0\u00a0\u00a0\u00a0 16\n2\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0A\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 98\u00a0\u00a0\u00a0 30\u00a0\u00a0\u00a0\u00a0\u00a0 24\n3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 B\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 80\u00a0\u00a0\u00a0 33\u00a0\u00a0\u00a0\u00a0\u00a0 32\n4\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 B\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 82\u00a0\u00a0\u00a0 33\u00a0\u00a0\u00a0\u00a0\u00a0 56\n5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 C\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 70\u00a0\u00a0\u00a0 33\u00a0\u00a0\u00a0\u00a0\u00a0 18\n6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 C\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 65\u00a0\u00a0\u00a0 23\u00a0\u00a0\u00a0\u00a0\u00a0 12<\/pre>\n\n\n\n<h3>Example 1: Find Unique Values in R<\/h3>\n\n\n\n<p>Use the unique() function to retrieve unique elements from a Vector, data frame, or array-like R object. <\/p>\n\n\n\n<p>The unique() function in R returns a vector, data frame, or array-like object with duplicate elements and rows deleted.<\/p>\n\n\n\n<p><a href=\"https:\/\/finnstats.com\/index.php\/2021\/05\/12\/visualization-graphs\/\" rel=\"nofollow\" target=\"_blank\">Data Visualization Graphs-ggside with ggplot \u00bb finnstats<\/a><\/p>\n\n\n\n<p>The code below demonstrates how to locate unique values in the \u2018Product\u2019 column.<\/p>\n\n\n\n<pre>unique(data$Product)\r\n[1] &quot;A&quot; &quot;B&quot; &quot;C&quot;<\/pre>\n\n\n\n<p>In the \u2018Score\u2019 column, we may use similar syntax to find unique values:<\/p>\n\n\n\n<pre>unique(data$Score)\r\n[1] 31 30 33 23<\/pre>\n\n\n\n<h3>Example 2: Find & Sort Unique Values in R<\/h3>\n\n\n\n<p>The code below demonstrates how to locate and sort unique values in the \u2018Score\u2019 column.<\/p>\n\n\n\n<p><a href=\"https:\/\/finnstats.com\/index.php\/2021\/05\/27\/datatable-editor-dt-package-in-r\/\" rel=\"nofollow\" target=\"_blank\">datatable editor-DT package in R \u00bb Shiny, R Markdown & R \u00bb finnstats<\/a><\/p>\n\n\n\n<pre>sort(unique(data$Score))\r\n[1] 23 30 31 33<\/pre>\n\n\n\n<p>In addition, we can sort unique values in descending order<\/p>\n\n\n\n<pre>sort(unique(data$Score), decreasing=TRUE)\r\n[1] 33 31 30 23<\/pre>\n\n\n\n<h3>Example 3: Find & Count Unique Values in R<\/h3>\n\n\n\n<p>The code below demonstrates how to locate and count the number of each distinct value in the \u2018Score\u2019 column.<\/p>\n\n\n\n<p><a href=\"https:\/\/finnstats.com\/index.php\/2021\/10\/30\/regression-in-r\/\" rel=\"nofollow\" target=\"_blank\">Regression Analysis Example-Ultimate Guide \u00bb finnstats<\/a><\/p>\n\n\n\n<pre>table(data$Score)\r\n23 30 31 33\n 1  1\u00a0 1\u00a0 3<\/pre>\n\n\n\n<p>The numbers 23, 30, and 31 appear once, whereas the number 33 appears three times, according to the above output.<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>In a vector, data frame, or array, the unique() function removes duplicate elements\/rows.<\/p>\n\n\n\n<p><a href=\"https:\/\/finnstats.com\/index.php\/2021\/11\/02\/whats-neural-network\/\" rel=\"nofollow\" target=\"_blank\">What is neural network in machine learning? \u00bb finnstats<\/a><\/p>\n<p>Don't forget to express your happiness by leaving a comment.<br \/>\n<a rel=\"nofollow\" href=\"https:\/\/finnstats.com\/index.php\/2021\/12\/15\/how-to-find-unique-values-in-r\/\" target=\"_blank\">How to Find Unique Values in R<\/a>.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/finnstats.com\/index.php\/2021\/12\/15\/how-to-find-unique-values-in-r\/\" target=\"_blank\">How to Find Unique Values in R<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/finnstats.com\/\" target=\"_blank\">finnstats<\/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:\/\/finnstats.com\/index.php\/2021\/12\/15\/how-to-find-unique-values-in-r\/\"> Methods \u2013 finnstats<\/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>Visit for the most up-to-date information on Data Science, employment, and tutorials finnstats.<br \/>\nIf you want to read the original article, go here How to Find Unique Values in R<br \/>\nTo find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data &#8230;<\/p>\n","protected":false},"author":2637,"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\/234189"}],"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\/2637"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=234189"}],"version-history":[{"count":5,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/234189\/revisions"}],"predecessor-version":[{"id":235345,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/234189\/revisions\/235345"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=234189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=234189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=234189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}