简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM