Kate
Kate

Reputation: 35

How to move any row to last row of data frame in r

How can I take a row in my data frame in R, and move it to the last row? I've seen commands that allow it to be moved to the top of the data frame, but have not been able to find one that moves it to the bottom of the data frame. Any help is appreciated, thank you.

Upvotes: 0

Views: 2983

Answers (4)

AlexB
AlexB

Reputation: 3269

Another way:

Last_Row <- function(df, row) {

  #just to make sure we do not select a row that is outside of df 
  stopifnot(nrow(df) >= row) 
  
  rbind(df[-row, ], df[row, ])
  
}

Assuming we deal with some information from iris data set:

df <- head(iris[, 1:3])

#    Sepal.Length Sepal.Width Petal.Length
# 1          5.1         3.5          1.4
# 2          4.9         3.0          1.4
# 3          4.7         3.2          1.3
# 4          4.6         3.1          1.5
# 5          5.0         3.6          1.4
# 6          5.4         3.9          1.7

Running Last_Row(df, 1) would generate:

#   Sepal.Length Sepal.Width Petal.Length
# 2          4.9         3.0          1.4
# 3          4.7         3.2          1.3
# 4          4.6         3.1          1.5
# 5          5.0         3.6          1.4
# 6          5.4         3.9          1.7
# 1          5.1         3.5          1.4

Upvotes: 3

GKi
GKi

Reputation: 39657

An option is using seq_len and sub setting with negative numbers.

x[c(seq_len(nrow(x))[-2],2),] #Move row 2 to the end
#  a b
#1 1 4
#3 3 2
#4 4 1
#2 2 3

and as a function:

move2last <- function(x, r) {
    n <- nrow(x)
    if(r >= n) x else x[c(seq_len(n)[-r],r),,drop = FALSE]
}
move2last(x, 2)
#  a b
#1 1 4
#3 3 2
#4 4 1
#2 2 3

Data

x <- data.frame(a = 1:4, b=4:1)

Upvotes: 0

deschen
deschen

Reputation: 10996

If you are fine with a tidyverse solution you can just use slice, e.g.

library(tidyverse)

dat <- data.frame(x = c(1, 2, 3, 4),
                  y = c(10, 9, 8, 7))

# Moving second row to last position
dat_new <- dat %>%
  slice(1, 3:4, 2)

which gives:

  x  y
1 1 10
2 3  8
3 4  7
4 2  9

Upvotes: 5

Ronak Shah
Ronak Shah

Reputation: 388982

Consider mtcars dataset as an example :

df <- head(mtcars, 10)
#                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

You can use this function to move any row to last position

move_to_last <- function(df, n) df[c(setdiff(seq_len(nrow(df)), n), n), ]
move_to_last(df, 4)

#                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1

move_to_last(df, 1)

#                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
#Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
#Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
#Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4

Upvotes: 4

Related Questions