# How to take values from one matrix by condition from the another?

Hi!

I have two matrices with the same structure. Matrix 1 contains significance values for values in Matrix 2.

Matrix1

CAN EU1 EU2 Z
CAN 500.5 1000.0 1000.0 1000.0
EU1 1000.0 500.5 1000.0 627.0
EU2 1000.0 1000.0 500.5 1000.0
Z 1000.0 627.0 1000.0 500.5

Matrix2

CAN EU1 EU2 Z
CAN NaN 3.7468 7.0960 5.1956
EU1 3.7468 NaN 4.0226 0.3671
EU2 7.0960 4.0226 NaN 3.9169
Z 5.1956 0.3671 3.9169 NaN

How can I take values from Matrix2 by condition from Matrix1 (significance level xij<50 or xij>950) and write them in new objects (e.g., CanEu1, CanEu2, Eu1Eu2 ...)? Here, I mean some code-realized resolves through loops or (possible) dplyr package, but I relatively newbie in R and don't know how to do this.

Thank you!

The snippet below illustrates using indexing to take advantage of the two objects having identical structures. It modifies the second object in place, although the operation could have been assigned to a new object instead. I did this because I'm not clear as to your intent.

1. This is a functional programming approach, which focuses on what rather than how. Every `R` problem can be thought of with advantage as the interaction of three objects— an existing object, x , a desired object,y , and a function, f, that will return a value of y given x as an argument. In other words, school algebra— f(x) = y. Any of the objects can be composites.

In this problem, x is the set of two matrices (in the form of data frames, but that is not an important distinction in this case since the values are all numeric; if any of the columns were character it would have to be data frames). I have defined y as one of the matricies transformed by adding 1 to the values appearing in the same index position as the other matrix under the condition that the value in the other matrix equal 627.

1. From the answer to the what question, the identification of f proceeds fairly smoothly. If we think of every function object, including operators, as a function, it needs only `[]`, `which`, `==` and `+` . The square brackets are subset operators. For a matrix they take as arguments the row and column indices, in that order. So `some_matrix[2,3]` returns the value of the third column in the second row. The equality and addition operators are obvious. `which` applies a logical test—in this case, is a value equal to 627? It returns the position or positions that satisfy the test.

Translating the composed function f into words—find the positions of matrix 1 that contain 627, add one to the values in matrix 2 in the same position and write the result back into matrix 2.

``````m1 <- data.frame(
CAN =
c(500.5, 1000, 1000, 1000),
EU1 =
c(1000, 500.5, 1000, 627),
EU2 =
c(1000, 1000, 500.5, 1000),
Z =
c(1000, 627, 1000, 500.5))

m2 <- data.frame(
CAN =
c(NaN, 3.7468, 7.096, 5.1956),
EU1 =
c(3.7468, NaN, 4.0226, 0.3671),
EU2 =
c(7.096, 4.0226, NaN, 3.9169),
Z =
c(5.1956, 0.3671, 3.9169, NaN))

m1
#>      CAN    EU1    EU2      Z
#> 1  500.5 1000.0 1000.0 1000.0
#> 2 1000.0  500.5 1000.0  627.0
#> 3 1000.0 1000.0  500.5 1000.0
#> 4 1000.0  627.0 1000.0  500.5
m2
#>      CAN    EU1    EU2      Z
#> 1    NaN 3.7468 7.0960 5.1956
#> 2 3.7468    NaN 4.0226 0.3671
#> 3 7.0960 4.0226    NaN 3.9169
#> 4 5.1956 0.3671 3.9169    NaN

m2[which(m1 == 627, arr.ind = TRUE)] = m2[which(m1 == 627, arr.ind = TRUE)] + 1

m2
#>      CAN    EU1    EU2      Z
#> 1    NaN 3.7468 7.0960 5.1956
#> 2 3.7468    NaN 4.0226 1.3671
#> 3 7.0960 4.0226    NaN 3.9169
#> 4 5.1956 1.3671 3.9169    NaN
``````

The above code is a `reprex`, which is how questions related to code are best presented. See the FAQ: How to do a minimal reproducible example `reprex` for beginners.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.