Is it possible to relocate rows in tidyverse framework like it is possible for columns with dplyr relocate?
In this example I would like to relocate row 1 to position 5 (end of dataframe)
My dataframe:
df <- structure(list(ID = c(1, 2, 3, 4, 5), var1 = c("a", "b", "c",
"d", "e"), var2 = c(1, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA,
-5L))
df
ID var1 var2
1 1 a 1
2 2 b 1
3 3 c 0
4 4 d 0
5 5 e 1
Desired output:
ID var1 var2
1 2 b 1
2 3 c 0
3 4 d 0
4 5 e 1
5 1 a 1
Note: In the it should be 'pipe friendly' solution. I tried a lot but found nothing. Thank you.
arrange()is the tidyverse verb for reordering rows. It can be (ab)used as follows:(
ID==1is logical; when it is orderedFALSEvalues come beforeTRUEvalues ...)This isn't as flexible as
relocate()(e.g. it's not immediately obvious how to say "move rows 100-200 so they are immediately after row 1000"), but you can probably find a way to do most tasks.Another option (less idiomatic in my opinion) is
slice():(this is a tidyverse translation of @akrun's base-R answer). Either of these solutions can also be written with pipes (e.g.
df %>% arrange(ID==1)).Just to be silly: