# R Program to sort a Data frame using order()

## How to sort a Data frame using order()

Here we explain how to write an R program to sort a data frame using the order() function. Here we are using a built-in function order() for this sorting. The order() function helps to returns a permutation. It rearranges its first argument into ascending or descending order, breaking ties by further arguments. The syntax of order() is like

``````order(…, na.last = TRUE, decreasing = FALSE,
```
```

Here dots(....) is the sequence of numeric, complex, character, or logical vectors or a classed R object. If na.last is TRUE, missing values in the data are put last; if FALSE, they are put first and the next is the method to be used here partial matches are allowed.

## How to sort a Data frame using order() in the R program

Below are the steps used in the R program to sort a Data frame using order() function. In this R program, we directly give the values to the built-in function. For that first, we use the seed() function that sets the starting number used to generate a sequence of random numbers. Here tibble indicates the data frames, and rnorm is the r function that simulates random variates having a specified normal distribution. Finally calling the function order() for sorting.

## ALGORITHM

STEP 1: Using seed() set starting numbers to generate a sequence

STEP 2: Using tibble define data frame

STEP 3: Consider variables c1,c2,c3,c4,c5 indicating data frames

STEP 4:Sort the data frames by calling order()

STEP 5: Assign the variable df with the function result

STEP 6: Print  the function result

## R Source Code

```                                          ```library(dplyr)
set.seed(1234)
data_frame <- tibble(
c1 = rnorm(50, 5, 1.5),
c2 = rnorm(50, 5, 1.5),
c3 = rnorm(50, 5, 1.5),
c4 = rnorm(50, 5, 1.5),
c5 = rnorm(50, 5, 1.5)
)
# Sort by c1
df <-data_frame[order(data_frame\$c1),]
```

## OUTPUT

```# A tibble: 6 x 5
##       c1       c2       c3       c4       c5
##
## 1 1.481453 3.477557 4.246283 3.686611 6.0511003
## 2 1.729941 5.824996 4.525823 6.753663 0.1502718
## 3 2.556360 6.275348 2.524849 6.368483 5.4787404
## 4 2.827693 4.769902 5.120089 3.743626 4.0103449
## 5 2.988510 4.395902 2.077631 4.236894 4.6176880
## 6 3.122021 6.317305 5.413840 3.551145 5.6067027```
VIEW ALL
VIEW ALL
VIEW ALL