user3664020
user3664020

Reputation: 3020

Swap rows in a data frame based on a criteria

I have a data.table something like this. Here is the dput of it.

structure(list(Sepal.Length = c(5.4, 5.1, 5, 5, 4.9, 4.9, 4.7, 
4.6, 4.6, 4.4, 5.4, 5.1, 5, 5, 4.9, 4.9, 4.7, 4.6, 4.6, 4.4), 
    Sepal.Width = c(3.9, 3.5, 3.6, 3.4, 3.1, 3, 3.2, 3.4, 3.1, 
    2.9, 3.9, 3.5, 3.6, 3.4, 3.1, 3, 3.2, 3.4, 3.1, 2.9), Petal.Length = c(1.7, 
    1.4, 1.4, 1.5, 1.5, 1.4, 1.3, 1.4, 1.5, 1.4, 1.7, 1.4, 1.4, 
    1.5, 1.5, 1.4, 1.3, 1.4, 1.5, 1.4), Petal.Width = c(0.4, 
    0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.2, 0.2, 0.4, 0.2, 0.2, 
    0.2, 0.1, 0.2, 0.2, 0.3, 0.2, 0.2), Species = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
    ), class = "factor"), order = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
    8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), .Names = c("Sepal.Length", 
"Sepal.Width", "Petal.Length", "Petal.Width", "Species", "order"
), row.names = c(NA, -20L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000000000340788>)

It looks like this. It is just a sample.

    Sepal.Length Sepal.Width Petal.Length Petal.Width Species order
 1:          5.4         3.9          1.7         0.4  setosa     1
 2:          5.1         3.5          1.4         0.2  setosa     2
 3:          5.0         3.6          1.4         0.2  setosa     3
 4:          5.0         3.4          1.5         0.2  setosa     4
 5:          4.9         3.1          1.5         0.1  setosa     5
 6:          4.9         3.0          1.4         0.2  setosa     6
 7:          4.7         3.2          1.3         0.2  setosa     7
 8:          4.6         3.4          1.4         0.3  setosa     8
 9:          4.6         3.1          1.5         0.2  setosa     9
10:          4.4         2.9          1.4         0.2  setosa    10
11:          5.4         3.9          1.7         0.4  setosa     1
12:          5.1         3.5          1.4         0.2  setosa     2
13:          5.0         3.6          1.4         0.2  setosa     3
14:          5.0         3.4          1.5         0.2  setosa     4
15:          4.9         3.1          1.5         0.1  setosa     5
16:          4.9         3.0          1.4         0.2  setosa     6
17:          4.7         3.2          1.3         0.2  setosa     7
18:          4.6         3.4          1.4         0.3  setosa     8
19:          4.6         3.1          1.5         0.2  setosa     9
20:          4.4         2.9          1.4         0.2  setosa    10

What I want is to swap those rows wherever column order's value is 1 and 2 (keeping column order as is but swap the remaining columns). So, in the above table, row 1 will get swapped with row 2 and row 11 will get swapped with row 12.

So the output will look like:

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species order
 1:          5.1         3.5          1.4         0.2  setosa     1
 2:          5.4         3.9          1.7         0.4  setosa     2
 3:          5.0         3.6          1.4         0.2  setosa     3
 4:          5.0         3.4          1.5         0.2  setosa     4
 5:          4.9         3.1          1.5         0.1  setosa     5
 6:          4.9         3.0          1.4         0.2  setosa     6
 7:          4.7         3.2          1.3         0.2  setosa     7
 8:          4.6         3.4          1.4         0.3  setosa     8
 9:          4.6         3.1          1.5         0.2  setosa     9
10:          4.4         2.9          1.4         0.2  setosa    10
11:          5.1         3.5          1.4         0.2  setosa     1
12:          5.4         3.9          1.7         0.4  setosa     2
13:          5.0         3.6          1.4         0.2  setosa     3
14:          5.0         3.4          1.5         0.2  setosa     4
15:          4.9         3.1          1.5         0.1  setosa     5
16:          4.9         3.0          1.4         0.2  setosa     6
17:          4.7         3.2          1.3         0.2  setosa     7
18:          4.6         3.4          1.4         0.3  setosa     8
19:          4.6         3.1          1.5         0.2  setosa     9
20:          4.4         2.9          1.4         0.2  setosa    10
> 

Please notice in the above output rows 1 and 2 and rows 11 and 12 of the original table have been swapped keeping column order as is. How do I do this in the most efficient manner i.e quickly without running any loop?

Upvotes: 0

Views: 120

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

Since you want to swap data, you will have to create a copy before you do the swap. In the code below, I create a df1 data.frame, a copy of df. I use it to fetch the right rows before inserting them in the original df. I only pick the first five columns to leave df$order intact.

df <-structure(list(Sepal.Length = c(5.4, 5.1, 5, 5, 4.9, 4.9, 4.7,
4.6, 4.6, 4.4, 5.4, 5.1, 5, 5, 4.9, 4.9, 4.7, 4.6, 4.6, 4.4),
    Sepal.Width = c(3.9, 3.5, 3.6, 3.4, 3.1, 3, 3.2, 3.4, 3.1,
    2.9, 3.9, 3.5, 3.6, 3.4, 3.1, 3, 3.2, 3.4, 3.1, 2.9), Petal.Length = c(1.7,
    1.4, 1.4, 1.5, 1.5, 1.4, 1.3, 1.4, 1.5, 1.4, 1.7, 1.4, 1.4,
    1.5, 1.5, 1.4, 1.3, 1.4, 1.5, 1.4), Petal.Width = c(0.4,
    0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.3, 0.2, 0.2, 0.4, 0.2, 0.2,
    0.2, 0.1, 0.2, 0.2, 0.3, 0.2, 0.2), Species = structure(c(1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
    1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
    ), class = "factor"), order = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
    8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), .Names = c("Sepal.Length",
"Sepal.Width", "Petal.Length", "Petal.Width", "Species", "order"
), row.names = c(NA, -20L), class = c("data.table", "data.frame"
))
df1 <-df

df[df$order==2,1:5] <-df1[df1$order==1,1:5]
df[df$order==1,1:5] <-df1[df1$order==2,1:5]
> df
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species order
1           5.1         3.5          1.4         0.2  setosa     1
2           5.4         3.9          1.7         0.4  setosa     2
3           5.0         3.6          1.4         0.2  setosa     3
4           5.0         3.4          1.5         0.2  setosa     4
5           4.9         3.1          1.5         0.1  setosa     5
6           4.9         3.0          1.4         0.2  setosa     6
7           4.7         3.2          1.3         0.2  setosa     7
8           4.6         3.4          1.4         0.3  setosa     8
9           4.6         3.1          1.5         0.2  setosa     9
10          4.4         2.9          1.4         0.2  setosa    10
11          5.1         3.5          1.4         0.2  setosa     1
12          5.4         3.9          1.7         0.4  setosa     2
13          5.0         3.6          1.4         0.2  setosa     3
14          5.0         3.4          1.5         0.2  setosa     4
15          4.9         3.1          1.5         0.1  setosa     5
16          4.9         3.0          1.4         0.2  setosa     6
17          4.7         3.2          1.3         0.2  setosa     7
18          4.6         3.4          1.4         0.3  setosa     8
19          4.6         3.1          1.5         0.2  setosa     9
20          4.4         2.9          1.4         0.2  setosa    10

Upvotes: 1

Related Questions