 # 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,
method = c("auto", "shell", "radix"))
```
```

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),]
```# A tibble: 6 x 5