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

July 29, 2021, Learn eTutorial
868

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

In this R program for sort a data frame, we are using the order() function. The order() function helps to return 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")) ```

• 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.

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 to 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