Home > OS >  Reorder data.table rows so that no two consecutive rows are the same
Reorder data.table rows so that no two consecutive rows are the same

Time:11-16

Given data with 2N rows with at least N unique rows, is there an elegant way to reorder so that each row is different from the one above it?

Here's a simple example

df <- data.table(
        a = rep(2, 4),
        b = c(2, 2, 1, 1)
    )

> df
   a b
1: 2 2
2: 2 2
3: 2 1
4: 2 1

Rows 2 breaks the rule as it is the same as row 1 which is just above it. Similarly row 4 breaks the rule.

Let's say reorder_function is the function that reorders the data, then

> reorder_function(df)
   a b
1: 2 2
2: 2 1
3: 2 2
4: 2 1

The output possibilities are not unique and it doesn't matter for my use case as long as the output satisfies the rule.

CodePudding user response:

Use rowid to "Generate unique row ids within each group", here defined by columns 'a' and 'b'. order data by the row id.

df[order(rowid(a, b))]
   a b
1: 2 2
2: 2 1
3: 2 2
4: 2 1
  • Related