How can I sort a data frame conditionally by multiple columns in R?

Thomas

I'm trying to sort different subsets of rows in a dataframe by different columns, depending on the value in another column. So, for instance, all rows with a given value in column D should be sorted by column B, while all rows with another value in column D should be sorted by column C. Here is an example data frame:

colA <- sample(LETTERS, 6)
colB <- sample(c(1:100), 6)
colC <- sample(c(101:200), 6)
condition <- rep(c("good", "bad"), each = 1, times = 3)
df <- data.frame(colA, colB, colC, condition)

>df
  colA colB colC condition
1    F   44  187      good
2    C   32  179       bad
3    A   93  191      good
4    U   66  146       bad
5    Q   72  156      good
6    O   92  124       bad

I would like to sort this data frame by colB if the condition is "bad" and by colC if the condition is "good", resulting in

> df_sorted
  colA colB colC condition
1    C   32  179       bad
2    U   66  146       bad
3    O   92  124       bad
4    Q   72  156      good
5    F   44  187      good
6    A   93  191      good

So far, I have been creating separate data frames for each condition, sorting them separately and then putting them back together with rbind. That approach works but is pretty tedious when there are a lot of different conditions. It seems there should be an easier way to do this, but I have not been able to find one. Any help would be most appreciated. Thank you!

r2evans

Perhaps this?

set.seed(42)
colA <- sample(LETTERS, 6)
colB <- sample(c(1:100), 6)
colC <- sample(c(101:200), 6)
condition <- rep(c("good", "bad"), each = 1, times = 3)
df <- data.frame(colA, colB, colC, condition)
df
#   colA colB colC condition
# 1    X   74  194      good
# 2    Z   14  126       bad
# 3    G   65  146      good
# 4    T   69  192       bad
# 5    O   44  200      good
# 6    K   97  112       bad

df[with(df, order(condition, ifelse(condition == "bad", colB, colC))),]
#   colA colB colC condition
# 2    Z   14  126       bad
# 4    T   69  192       bad
# 6    K   97  112       bad
# 3    G   65  146      good
# 1    X   74  194      good
# 5    O   44  200      good

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I add columns in a data frame?

How can I select and rename a long list of columns from a data frame in R?

How can I rename all columns of a data frame based on another data frame in R?

How can I aggregate multiple columns in a data.frame with a custom function in R?

How to sort part of a data frame by multiple columns in R?

in R, how can I calculate y/y and w/w in a data frame, based on different columns criteria?

How to merge data frame with multiple columns with R?

How can I compare two rows in R data frame by different columns and perform an operation on them?

In R, how can I combine two columns within a data.frame

Sort a data frame in R by multiple columns at the same time

Exporting R data frame to CSV - How can I quote all columns including numeric ones?

How can I use if else to change values in some rows and columns in my data frame in R?

How can I access columns by name in an anonymous data frame object for filtering in R?

How can I remove observations from a data frame conditionally without losing NA values in R?

In R, how can I merge two data.frame columns using parentheses () as separation?

How can I summarize a data frame in a "long" format by multiple columns?

how can i restructure the data frame in R

How can I use between function for entire columns of data.frame in R?

How can I drop obs. conditionally to another data frame? R

How can I conditionally add columns (string) together in pandas Data Frame?

How can I conditionally edit strings in pandas data frame?

Sort (order) data frame rows by multiple columns

How to conditionally insert rows into a data frame with R?

How to sort multiple columns in R data frame from smallest to largest by group?

How I can calculate the variance across all columns in a data frame in R according to the values of another data frame using Dplyr?

How can I rollapply in R with two columns of a data frame?

How can I conditionally subset a data frame using a function in R?

How can I identify different combinations of binary variables stored in columns in a data frame using R

How can I take repeating rows and transform the data frame to add columns of unique identifiers in R?