Sorting a data frame by multiple variables

Kellan Baker

I have a data frame with 4 groups (defined by categories "a" and "b" in column 1 and categories "X" and "Y" in column 2). I want to rank the attributes in column 3 by their values in column 4, but specifically within the groups in columns 1 and 2 (AX, AY, BX, BY).

How can I go from this:

col1    col2    col3    col4
a       X       pat     1
b       Y       dog     2
b       X       leg     3
a       X       hog     4                   
b       Y       egg     5
a       Y       log     6
b       X       map     7
b       Y       ice     8
b       X       mat     9
a       Y       sat     10

to this?

col1    col2    col3    col4
a       X       hog     4
a       X       pat     1
a       Y       sat     10
a       Y       log     6                   
b       X       mat     9
b       X       map     7
b       X       leg     3
b       Y       ice     8
b       Y       egg     5
b       Y       dog     2

(example input code below)

col1 <- c('a','b','b','a','b','a','b','b','b','a')
col2 <- c('X','Y','X','X','Y','Y','X','Y','X','Y')
col3 <- c('pat','dog','leg','hog','egg','log','map','ice','mat','sat')
col4 <- c(1,2,3,4,5,6,7,8,9,10)

df <- data.frame(col1,col2,col3,col4)

colA <- c('a','a','a','a','b','b','b','b','b','b')
colB <- c('X','X','Y','Y','X','X','X','Y','Y','Y')
colC <- c('hog','pat','sat','log','mat','map','leg','ice','egg','dog')
colD <- c(4,1,10,6,9,7,3,8,5,2)

df1 <- data.frame(colA,colB,colC,colD)

I tried the following, but it gives a random arrangement that has none of the ranked-within-groups structure that I want:

df %>% group_by(col1, col2) %>% arrange(desc(col4)) 

df %>% group_by(col1) %>% arrange(col1) %>% group_by(col2) %>% arrange(col2) sorts the data frame correctly by the first two columns, but I can't further arrange it by col4.

akrun

For this, we don't need group_by

library(dplyr)
df %>%
    arrange(col1, col2, desc(col4))
#   col1 col2 col3 col4
#1     a    X  hog    4
#2     a    X  pat    1
#3     a    Y  sat   10
#4     a    Y  log    6
#5     b    X  mat    9
#6     b    X  map    7
#7     b    X  leg    3
#8     b    Y  ice    8
#9     b    Y  egg    5
#10    b    Y  dog    2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Changing attributes of multiple variables in data frame

Sorting according to duplication with multiple subset of duplication within a pandas data frame

sorting pandas data frame based on multiple values in a single cell

Regressing a data frame of multiple dependent variables on a data frame of multiple explanatory variables

Sorting a data frame with conditions

assign multiple categorical values to series of data frame variables in r

Ordering data.frame by multiple variables in mixed direction

Hourly mean of multiple variables in R data.frame?

Delete data frame lines with multiple variables values conditions R

R: Parse data.frame string variable into multiple variables

Data frame from wide to long with multiple variables and ids R

Same action on multiple variables and make into a data frame in the end

Plotting a time series graph with multiple variables representing date in a data frame

Aggregating (subtotals) in data frame with multiple factor (character) variables

Open file with multiple spaces and save as array/data frame with two variables

Identify unique raws for a data.frame that is grouped by multiple variables

Correlation between multiple variables of a data frame Group by a different variable

R: Sorting a data frame by an alphanumeric

Sorting a pandas data frame by a series

python, dictionary in a data frame, sorting

Double sorting in data.frame

Changing variables in data frame

Sorting multiple data members

Sorting Data in Multiple Columns

In two data.frames in R, find values in one data.frame, based on multiple variables in the other data.frame

Multiple columns data frame

How to filter multiple variables in a data.frame/data.table in Shiny with various combinations

Custom Data Frame Using Variables from Multiple Data Frames and Merging Filtered Results - Reprex Available

Sorting R Data Frame in a Specified Order