{"id":7236,"date":"2018-10-01T21:34:27","date_gmt":"2018-10-01T21:34:27","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=7236"},"modified":"2018-10-19T07:21:05","modified_gmt":"2018-10-19T05:21:05","slug":"reorder-data-frame-rows-in-r","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/","title":{"rendered":"Reorder Data Frame Rows in R"},"content":{"rendered":"<div id=\"rdoc\">\n<p>This tutorial describes how to <strong>reorder<\/strong> (i.e., <strong>sort<\/strong>) rows, in your data table, by the value of one or more columns (i.e., variables).<\/p>\n<p>You will learn how to easily:<\/p>\n<ul>\n<li>Sort a data frame rows in <em>ascending order<\/em> (from low to high) using the R function <strong>arrange<\/strong>() [<strong>dplyr<\/strong> package]<\/li>\n<li>Sort rows in <em>descending order<\/em> (from high to low) using <strong>arrange<\/strong>() in combination with the function <strong>desc<\/strong>() [<strong>dplyr<\/strong> package]<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/data-manipulation-in-r\/images\/reorder-data-frame-rows-in-r.png\" alt=\"Reorder Data Frame Rows by Variables in R\" \/><\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#required-packages\">Required packages<\/a><\/li>\n<li><a href=\"#demo-dataset\">Demo dataset<\/a><\/li>\n<li><a href=\"#arrange-rows\">Arrange rows<\/a><\/li>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<\/ul>\n<\/div>\n<div id=\"required-packages\" class=\"section level2\">\n<h2>Required packages<\/h2>\n<p>Load the <code>tidyverse<\/code> packages, which include <code>dplyr<\/code>:<\/p>\n<pre class=\"r\"><code>library(tidyverse)<\/code><\/pre>\n<\/div>\n<div id=\"demo-dataset\" class=\"section level2\">\n<h2>Demo dataset<\/h2>\n<p>We\u2019ll use the R built-in iris data set, which we start by converting into a tibble data frame (tbl_df) for easier data analysis.<\/p>\n<pre class=\"r\"><code>my_data &lt;- as_tibble(iris)\r\nmy_data<\/code><\/pre>\n<pre><code>## # A tibble: 150 x 5\r\n##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species\r\n##          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;  \r\n## 1          5.1         3.5          1.4         0.2 setosa \r\n## 2          4.9         3            1.4         0.2 setosa \r\n## 3          4.7         3.2          1.3         0.2 setosa \r\n## 4          4.6         3.1          1.5         0.2 setosa \r\n## 5          5           3.6          1.4         0.2 setosa \r\n## 6          5.4         3.9          1.7         0.4 setosa \r\n## # ... with 144 more rows<\/code><\/pre>\n<\/div>\n<div id=\"arrange-rows\" class=\"section level2\">\n<h2>Arrange rows<\/h2>\n<p>The dplyr function <code>arrange()<\/code> can be used to reorder (or sort) rows by one or more variables.<\/p>\n<ul>\n<li>Reorder rows by Sepal.Length in <strong>ascending<\/strong> order<\/li>\n<\/ul>\n<pre class=\"r\"><code>my_data %&gt;% arrange(Sepal.Length)<\/code><\/pre>\n<pre><code>## # A tibble: 150 x 5\r\n##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species\r\n##          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;  \r\n## 1          4.3         3            1.1         0.1 setosa \r\n## 2          4.4         2.9          1.4         0.2 setosa \r\n## 3          4.4         3            1.3         0.2 setosa \r\n## 4          4.4         3.2          1.3         0.2 setosa \r\n## 5          4.5         2.3          1.3         0.3 setosa \r\n## 6          4.6         3.1          1.5         0.2 setosa \r\n## # ... with 144 more rows<\/code><\/pre>\n<ul>\n<li>Reorder rows by Sepal.Length in <strong>descending<\/strong> order. Use the function <strong>desc<\/strong>():<\/li>\n<\/ul>\n<pre class=\"r\"><code>my_data %&gt;% arrange(desc(Sepal.Length))<\/code><\/pre>\n<pre><code>## # A tibble: 150 x 5\r\n##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species  \r\n##          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;    \r\n## 1          7.9         3.8          6.4         2   virginica\r\n## 2          7.7         3.8          6.7         2.2 virginica\r\n## 3          7.7         2.6          6.9         2.3 virginica\r\n## 4          7.7         2.8          6.7         2   virginica\r\n## 5          7.7         3            6.1         2.3 virginica\r\n## 6          7.6         3            6.6         2.1 virginica\r\n## # ... with 144 more rows<\/code><\/pre>\n<div class=\"success\">\n<p>Instead of using the function <strong>desc<\/strong>(), you can prepend the sorting variable by a minus sign to indicate descending order, as follow.<\/p>\n<\/div>\n<pre class=\"r\"><code>arrange(my_data, -Sepal.Length)<\/code><\/pre>\n<ul>\n<li>Reorder rows by <strong>multiple variables<\/strong>: Sepal.Length and Sepal.width<\/li>\n<\/ul>\n<pre class=\"r\"><code>my_data %&gt;% arrange(Sepal.Length, Sepal.Width)<\/code><\/pre>\n<div class=\"notice\">\n<p>If the data contain missing values, they will always come at the end.<\/p>\n<\/div>\n<\/div>\n<div id=\"summary\" class=\"section level2\">\n<h2>Summary<\/h2>\n<p>In this article, we describe how to sort data frame rows using the function <code>arrange()<\/code> [<strong>dplyr<\/strong> package].<\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial describes how to reorder rows, in your data table, by the value of one or more variables. You will learn how to easily sort a data frame rows in ascending  and descending orders.<\/p>\n","protected":false},"author":1,"featured_media":7729,"parent":0,"menu_order":4,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-7236","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry","lesson_complexity-easy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Reorder Data Frame Rows in R - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reorder Data Frame Rows in R - Datanovia\" \/>\n<meta property=\"og:description\" content=\"This tutorial describes how to reorder rows, in your data table, by the value of one or more variables. You will learn how to easily sort a data frame rows in ascending and descending orders.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2018-10-19T05:21:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/\",\"name\":\"Reorder Data Frame Rows in R - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg\",\"datePublished\":\"2018-10-01T21:34:27+00:00\",\"dateModified\":\"2018-10-19T05:21:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reorder Data Frame Rows in R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reorder Data Frame Rows in R - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/","og_locale":"en_US","og_type":"article","og_title":"Reorder Data Frame Rows in R - Datanovia","og_description":"This tutorial describes how to reorder rows, in your data table, by the value of one or more variables. You will learn how to easily sort a data frame rows in ascending and descending orders.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/","og_site_name":"Datanovia","article_modified_time":"2018-10-19T05:21:05+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/","name":"Reorder Data Frame Rows in R - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg","datePublished":"2018-10-01T21:34:27+00:00","dateModified":"2018-10-19T05:21:05+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/10\/IMG_0312.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/reorder-data-frame-rows-in-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"Reorder Data Frame Rows in R"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/7236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=7236"}],"version-history":[{"count":0,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/7236\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/7729"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=7236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}