TarJae
TarJae

Reputation: 79311

R: Reorder data frame rows by row index

Suppose I have a given dataframe like in the example. How can I reorder the rows so that row number 2 is at the end of the dataframe. Ideally with dplyr. Thanks! example

mycode:

name <- c("Jon", "Bill", "Maria", "hans")
age <- c(23, 41, 32, 66)
something <- c(1,2,3, 6)
something_more <- c(4,5,6, 9)

df <- data.frame(name, age, something, something_more )


Upvotes: 0

Views: 861

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389325

You can use slice to drop/select specific rows.

library(dplyr)

df %>% slice(-2) %>% bind_rows(df %>% slice(2))

#   name age something something_more
#1   Jon  23         1              4
#2 Maria  32         3              6
#3  hans  66         6              9
#4  Bill  41         2              5

Upvotes: 4

Related Questions