1

I have a dataframe with several "people".
There are repeat instances for "people", however, the measured "value" is different in each instance.

Here is an example of dataframe.

df2 <- data.frame(
  value = c(1, 2, 3, 4, 5), 
  people = c("d", "c", "b", "d", "b")
)

which looks like:

  value people
     1      d
     2      c
     3      b
     4      d
     5      b

I would like to group the data by "people", then sort the groups of rows by "value", and within the groups, I would like to sort descending by the "value".

That is, I want to keep duplicates together while sorting by value.

Here is how I would like the data to look:

  value people
     1      d
     4      d
     2      c
     3      b
     5      b 

I have tried multiple attempts with group_by and arrange using {dplyr} but seems I am missing something.
Thanks for the help.

I have made a change - for clarity, I do not want "people" sorted alphabetically - this is a schedule in reality - person D has the first appointment (1), and his second appointment is 4. I want them to appear first and together. Person C has a 2nd appointment. Person B has a 3rd appointment, his other appointment is 5. I hope this makes it more clear. Thanks again

kgangadhar
  • 3,643
  • 3
  • 24
  • 46
mdb_ftl
  • 221
  • 1
  • 10
  • 1
    Don't you want `df2 %>% arrange(people, value)` ? – Ronak Shah Dec 16 '20 at 04:55
  • df2 %>% arrange(people, value) does not quite get what I am looking for - sorry i made an edit to original post, there was a typo – mdb_ftl Dec 16 '20 at 04:57
  • 1
    So you don't want to change the order of `people`. If `d` occurred first it should remain as it is in the final data as well? – Ronak Shah Dec 16 '20 at 04:58
  • exactly, - i do not want people arrange alphabetically - could be ANY names - i want them to occur in the order of value, but i want repeats instances of people to be together – mdb_ftl Dec 16 '20 at 05:00
  • 1
    Then why is `c` second person? Shouldn't it be `b` ? – Ronak Shah Dec 16 '20 at 05:02
  • Should the groups itself be sorted on sum(value) for each group? – AnilGoyal Dec 16 '20 at 05:03
  • I changed the original dataframe so that i does not appear that I want them sorted alphabetically thanks – mdb_ftl Dec 16 '20 at 05:09
  • If you know how you want the people sorted, then you can make people a factor with the levels in that order, and then it will sort correctly. Perhaps using https://forcats.tidyverse.org/reference/fct_reorder.html – Dan Slone Dec 16 '20 at 05:23

3 Answers3

3

You can use arrange in this form :

library(dplyr)

df2 %>% 
  arrange(value) %>%
  arrange(match(people, unique(people)))

#  value people
#1     1      d
#2     4      d
#3     2      c
#4     3      b
#5     5      b
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
1

Though a longer code, but this will also work

df2 %>% group_by(people) %>% arrange(value) %>%
 mutate(d = first(value)) %>% arrange(d) %>% ungroup() %>% select(-d)

# A tibble: 5 x 2
  value people
  <dbl> <chr> 
1     1 d     
2     4 d     
3     2 c     
4     3 b     
5     5 b 
AnilGoyal
  • 14,820
  • 3
  • 16
  • 30
0

I got your result with the following one-liner base-R code:

df2[order(df2$people, decreasing = TRUE),]

#  value people
# 1     1      d
# 4     4      d
# 2     2      c
# 3     3      b
# 5     5      b
Abdur Rohman
  • 379
  • 5