Home > OS >  Renumbering ordered sequence in R and then use the mappings
Renumbering ordered sequence in R and then use the mappings

Time:05-22

dt <- structure(list(X2 = c(-1, -1, 2, 2, 2, -5, 10, 16, 19, 10, -9, 
-9, 23, 28, -21), X3 = c(-5, -9, 10, 12, 16, -21, 19, 20, 23, 
24, -26, -27, 28, 29, -30)), class = "data.frame", row.names = c(1L, 
2L, 3L, 4L, 5L, 8L, 6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L))

Let's say we have a dt like this

    X2  X3
1   -1  -5
2   -1  -9
3    2  10
4    2  12
5    2  16
6   -5 -21
7   10  19
8   16  20
9   19  23
10  10  24
11  -9 -26
12  -9 -27
13  23  28
14  28  29
15 -21 -30

At the second column, I want to renumber -5 to -1, -9 to -2, 10 to 3, 12 to 4, 16 to 5, -21 to -6, etc..., and let's say they are the mappings.

The rule is to renumber the numbers in the second column while retaining their signs, to make dt become this:

    X2  X3
1   -1  -1
2   -1  -2
3    2   3
4    2   4
5    2   5
6   -5  -6
7   10   7
8   16   8
9   19   9
10  10  10
11  -9 -11
12  -9 -12
13  23  13
14  28  14
15 -21 -15

After this, I want to renumber the first column according to the mappings in the previous step, if the mappings exist:

    X2  X3
1   -1  -1
2   -1  -2
3    2   3
4    2   4
5    2   5
6   -1  -6
7    3   7
8    5   8
9    7   9
10   3  10
11  -2 -11
12  -2 -12
13   9  13
14  13  14
15  -6 -15

CodePudding user response:

The first step of creating a "lookup" can be done by multiplying seq_along(dt$X3) by sign(dt$X3).

My understanding of the second step is that you want to find the indices of the original X3 where each entry in X2 is present. If there is no match, we keep X2 unchanged, otherwise we replace the value of the lookup at the matched index.

Assuming I have interpreted this correctly, we can do

lookup <- seq_along(dt$X3) * sign(dt$X3)
data.frame(X2 = ifelse(is.na(match(dt$X2, dt$X3)), 
                       dt$X2, lookup[match(dt$X2, dt$X3)]),
           X3 = lookup)

#>    X2  X3
#> 1  -1  -1
#> 2  -1  -2
#> 3   2   3
#> 4   2   4
#> 5   2   5
#> 6  -1  -6
#> 7   3   7
#> 8   5   8
#> 9   7   9
#> 10  3  10
#> 11 -2 -11
#> 12 -2 -12
#> 13  9  13
#> 14 13  14
#> 15 -6 -15

Created on 2022-05-21 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related