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"))
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.
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
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),]
head(df)
# 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