Reputation: 77
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!
Upvotes: 1
Views: 175
Reputation: 160792
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
Upvotes: 1