hklovs
hklovs

Reputation: 642

Organize numbers in group ascending

Sorry for the mess, I´ll start from the beginning. I want this tibble:

A tibble: 20 x 2

  ID NAME 
   <dbl> <chr>
 1     1 aa   
 2     1 bb   
 3     1 cc   
 4     2 dd   
 5     2 ee   
 6     2 ff   
 7     3 gg   
 8     3 hh   
 9     3 ii   
10     4 jj   
11     4 kk   
12     4 ll   
13     5 mm   
14     5 nn   
15     5 oo   
16     6 pp   
17     7 qq   
18     8 rr   
19     9 ss   
20    10 tt   

Organized in this manner:

A tibble: 20 x 2

  ID NAME 
   <dbl> <chr>
 1     1 aa   
 2     2 dd   
 3     3 gg   
 4     4 jj   
 5     5 mm   
 6     6 pp   
 7     7 qq   
 8     8 rr   
 9     9 ss   
10    10 tt   
11     1 bb   
12     2 ee   
13     3 hh   
14     4 kk   
15     5 nn   
16     1 cc   
17     2 ff   
18     3 hh   
19     4 ll   
20     5 oo   

That is the first '1' followed by the first '2' and so on..

Thanks for all answers but I think my example was perhaps a bit misleading.

Best regards,

H

Upvotes: 1

Views: 50

Answers (2)

akrun
akrun

Reputation: 887691

Here, the 'NAME' seems to be ordered in a specific order. In that case, convert to factor, with levels specified in that custom order and pass it on to arrange and then do the order with 'ID'

library(dplyr)
df1 %>%
    arrange(factor(NAME, levels = c("xx", "zz", "yy")), ID)
#   ID NAME
#1   1   xx
#2   2   xx
#3   3   xx
#4   4   xx
#5   5   xx
#6   6   xx
#7   7   xx
#8   8   xx
#9   9   xx
#10 10   xx
#11  1   zz
#12  2   zz
#13  3   zz
#14  4   zz
#15  5   zz
#16  1   yy
#17  2   yy
#18  3   yy
#19  4   yy
#20  5   yy

Or as per @zx8574 post, if it is the order of occurrence of values in 'NAME', either use the factor approach mentioned in his post or another way is match

df1 %>% 
     arrange(match(NAME, unique(NAME)), ID)

Update

Based on the updated data, we could reorder based on the sequence grouped by 'ID'

library(data.table)
df2 %>% 
     arrange(rowid(ID))
#   ID NAME
#1   1   aa
#2   2   dd
#3   3   gg
#4   4   jj
#5   5   mm
#6   6   pp
#7   7   qq
#8   8   rr
#9   9   ss
#10 10   tt
#11  1   bb
#12  2   ee
#13  3   hh
#14  4   kk
#15  5   nn
#16  1   cc
#17  2   ff
#18  3   ii
#19  4   ll
#20  5   oo

Or using data.table

setDT(df2)[order(rowid(ID))]

data

df1 <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L, 6L, 7L, 8L, 9L, 10L), NAME = c("xx", "zz", 
"yy", "xx", "zz", "yy", "xx", "zz", "yy", "xx", "zz", "yy", "xx", 
"zz", "yy", "xx", "xx", "xx", "xx", "xx")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20"))


df2 <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L, 6L, 7L, 8L, 9L, 10L), NAME = c("aa", "bb", 
"cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", 
"nn", "oo", "pp", "qq", "rr", "ss", "tt")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20"))

Upvotes: 2

zx8754
zx8754

Reputation: 56219

Order by two columns, while keeping the order of NAME using factor:

df1[ order(factor(df1$NAME, levels = unique(df1$NAME)), df1$ID), ]
#    ID NAME
# 1   1   xx
# 4   2   xx
# 7   3   xx
# 10  4   xx
# 13  5   xx
# 16  6   xx
# 17  7   xx
# 18  8   xx
# 19  9   xx
# 20 10   xx
# 2   1   zz
# 5   2   zz
# 8   3   zz
# 11  4   zz
# 14  5   zz
# 3   1   yy
# 6   2   yy
# 9   3   yy
# 12  4   yy
# 15  5   yy

data

df1 <- read.table(text = "
ID NAME 
1 xx   
1 zz   
1 yy   
2 xx   
2 zz   
2 yy   
3 xx   
3 zz   
3 yy   
4 xx   
4 zz   
4 yy   
5 xx   
5 zz   
5 yy   
6 xx   
7 xx   
8 xx   
9 xx   
10 xx", header = TRUE, stringsAsFactors = FALSE)

Upvotes: 1

Related Questions