Search code examples
rlistrescale

Rescale one list of matrices to another


I have two lists of 2-columns matrices like these:

> stck.list
[[1]]
             [,1]         [,2]
[1,]  0.000000000  0.000000000
[2,]  0.001082767 -0.008137698
[3,] -0.005346008  0.013462373
[4,]  0.012179531  0.046037670
[5,]  0.035469135  0.033196833

[[2]]
             [,1]         [,2]
[1,]  0.000000000 0.0000000000
[2,]  0.004100097 0.0001743182
[3,]  0.001005368 0.0008254498
[4,]  0.011574428 0.0077799754
[5,] -0.006285223 0.0063603749

> stck.list.2
[[1]]
            [,1]         [,2]
[1,] 0.000000000  0.000000000
[2,] 0.003678552  0.009008164
[3,] 0.007416574  0.003603477
[4,] 0.005194380 -0.004445452
[5,] 0.002181469 -0.005857811

[[2]]
              [,1]          [,2]
[1,]  0.0000000000  0.0000000000
[2,]  0.0020240535 -0.0011925407
[3,] -0.0003965362  0.0013492189
[4,]  0.0053106932  0.0001212016
[5,]  0.0023810324  0.0054779249

And i need to rescale values from first column from first matrix from first list to first column from first matrix from second list, e.t.c. Manually i can do it for each column separately for one matrix like that:

sc_x.1 <- scales::rescale(stck.list[[1]][,1], to = range(stck.list.2[[1]][,1]))
sc_y.1 <- scales::rescale(stck.list[[1]][,2], to = range(stck.list.2[[1]][,2]))

Is it real to do it? So the main goal is to dotplot each corresponding matrices from two lists on corresponding axes. Maybe there is an easier way to do this?


Solution

  • We can use Map

    Map(function(x, y) scales::rescale(x, to = range(y)) , stck.list, stck.list.2)