Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rearrange a data frame by sorting a column within groups

Tags:

sorting

r

What is a good way to perform the following task?

I have a data frame, for example:

v2 <- c(4.5, 2.5, 3.5, 5.5, 7.5, 6.5, 2.5, 1.5, 3.5)
v1 <- c(2.2, 3.2, 1.2, 4.2, 2.2, 3.2, 2.2, 1.2, 5.2)
lvl <- c("a","a","a","b","b","b","c","c","c")
d <- data.frame(v1,v2,lvl)

> d
   v1  v2 l
1 2.2 4.5 a
2 3.2 2.5 a
3 1.2 3.5 a
4 4.2 5.5 b
5 2.2 7.5 b
6 3.2 6.5 b
7 2.2 2.5 c
8 1.2 1.5 c
9 5.2 3.5 c

Within each level of d$lvl, I want to sort the data frame by value of d$v1. So I want to get

   v1  v2 l
3 1.2 3.5 a
1 2.2 4.5 a
2 3.2 2.5 a

5 2.2 7.5 b
6 3.2 6.5 b
4 4.2 5.5 b

8 1.2 1.5 c
7 2.2 2.5 c
9 5.2 3.5 c
like image 512
user2783615 Avatar asked Sep 16 '13 23:09

user2783615


People also ask

How do you order a data frame from a specific column?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.

How do you sort a data frame by multiple columns?

You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.

How do I sort an entire data frame?

In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.

How do I sort a pandas DataFrame by one column?

Pandas DataFrame – Sort by Column To sort the rows of a DataFrame by a column, use pandas. DataFrame. sort_values() method with the argument by=column_name . The sort_values() method does not modify the original DataFrame, but returns the sorted DataFrame.


2 Answers

I think the most straightforward way is

d[order(d$lvl,d$v1),]

which gives

   v1  v2 lvl
3 1.2 3.5   a
1 2.2 4.5   a
2 3.2 2.5   a
5 2.2 7.5   b
6 3.2 6.5   b
4 4.2 5.5   b
8 1.2 1.5   c
7 2.2 2.5   c
9 5.2 3.5   c
like image 159
Frank Avatar answered Oct 30 '22 16:10

Frank


Using dplyr, you can add .by_group = TRUE to arrange() to sort the column within each group. Try:

library(dplyr)
d %>% 
        group_by(lvl) %>%
        arrange(v1, .by_group = TRUE)
# output
# A tibble: 9 x 3
# Groups:   lvl [3]
     v1    v2    lvl
  <dbl> <dbl> <fctr>
1   1.2   3.5      a
2   2.2   4.5      a
3   3.2   2.5      a
4   2.2   7.5      b
5   3.2   6.5      b
6   4.2   5.5      b
7   1.2   1.5      c
8   2.2   2.5      c
9   5.2   3.5      c
like image 28
nghauran Avatar answered Oct 30 '22 15:10

nghauran