Home > Net >  Ordering column of dataframe by group in different directions
Ordering column of dataframe by group in different directions

Time:09-21

I have an example dataframe:

dd <- data.frame(a = factor(c("Hi", "Hi", "Low", "Low"), 
                 levels = c("Low", "Hi"), ordered = T),
                 b = c(1,2,3,4))
dd
#     a b
# 1  Hi 1
# 2  Hi 2
# 3 Low 3
# 4 Low 4

I'd like to order column b in ascending order if column a has "Hi" as its value, and I'd like to order column b in descending if column a has "Low" as its value. I'd appreciate any help!

CodePudding user response:

One option is to make use of desc and rev based on the value of 'a'

library(dplyr)
dd %>% 
    arrange(match(a, c("Hi", "Low")),
          case_when(a == "Low"  ~ desc(b), TRUE ~ rev(desc(b))))

-output

    a b
1  Hi 1
2  Hi 2
3 Low 4
4 Low 3

Or another option is to convert the numeric column by changing the sign based on the values in 'a'

dd %>% 
    arrange(match(a, c("Hi", "Low")), b * c(1, -1)[1   (a == "Low")])
    a b
1  Hi 1
2  Hi 2
3 Low 4
4 Low 3

CodePudding user response:

We could use dplyr::arrange with an ifelse condition. Instead of using desc we could use base R rev function:

library(dplyr)
dd %>% 
    arrange(a, ifelse(a == "Hi", b, rev(b))) %>% 
    arrange(rev(a))

output:

    a b
1  Hi 1
2  Hi 2
3 Low 4
4 Low 3
  •  Tags:  
  • r
  • Related