sort dataframe by column r code example

Example 1: sort a dataframe by a column valuepython

>>> df.sort_values(by=['col1'])
    col1 col2 col3
0   A    2    0
1   A    1    1
2   B    9    9
5   C    4    3
4   D    7    2
3   NaN  8    4

Example 2: sort in descending order in r

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)

Example 3: r sort data frame by one column

population[order(population$age),]

Example 4: sort in descending order in r

#sort by mpg (ascending) and cyl (descending)
newdata <- mtcars[order(mpg, -cyl),]

Example 5: sort dataframe r

df %>% arrange(column)