# R: union of matrices every time a row is from one matrix with all rows in another matrix

I have 2 females ( `expmat`

and `mat`

). Let's say`expmat`

`````` [1,]   -1   -1   -1   -1   -1
[2,]    1   -1   -1   -1   -1
[3,]   -1    1   -1   -1   -1
[4,]    1    1   -1   -1   -1
[5,]   -1   -1    1   -1   -1
[6,]    1   -1    1   -1   -1
[7,]   -1    1    1   -1   -1
[8,]    1    1    1   -1   -1
```

```

and `mat`

there is

``````[1,]   -2   -2   -2   -2   -2
[2,]    2   -2   -2   -2   -2
[3,]   -2    2   -2   -2   -2
[4,]    2    2   -2   -2   -2
[5,]   -2   -2    2   -2   -2
[6,]    2   -2    2   -2   -2
[7,]   -2    2    2   -2   -2
```

```

I would like to create a loop where in each loop I join one row from `expmat`

to all rows in the mat so that at the end 8 matrices are created, and in each matrix at first 5 columns are exactly the same (row from `expmat`

) and other columns change. since they come from the mat. For example, the first 2 matrices of the given example will look like

``````Matrix1
-1   -1   -1   -1   -1   -2   -2   -2   -2   -2
-1   -1   -1   -1   -1    2   -2   -2   -2   -2
-1   -1   -1   -1   -1   -2    2   -2   -2   -2
-1   -1   -1   -1   -1    2    2   -2   -2   -2
-1   -1   -1   -1   -1   -2   -2    2   -2   -2
-1   -1   -1   -1   -1    2   -2    2   -2   -2
-1   -1   -1   -1   -1   -2    2    2   -2   -2

Matrix 2
1   -1   -1   -1   -1   -2   -2   -2   -2   -2
1   -1   -1   -1   -1    2   -2   -2   -2   -2
1   -1   -1   -1   -1   -2    2   -2   -2   -2
1   -1   -1   -1   -1    2    2   -2   -2   -2
1   -1   -1   -1   -1   -2   -2    2   -2   -2
1   -1   -1   -1   -1    2   -2    2   -2   -2
1   -1   -1   -1   -1   -2    2    2   -2   -2
```

```

This is what I am doing:

``````for(row in 1:nrow(expmat)) {
for(row in 1:nrow(expmat)) {
val<-expmat[row,]
val<-as.matrix(val)
dshybrid=merge(t(val),mat, by=0, all=TRUE)
print(dshybrid)}
```

```

However, I get what I need instead

``````-1   -1   -1   -1   -1   -2   -2   -2   -2   -2
NA     NA     NA     NA     NA   2   -2   -2   -2   -2
NA     NA     NA     NA     NA   -2    2   -2   -2   -2
NA     NA     NA     NA     NA    2    2   -2   -2   -2
NA     NA     NA     NA     NA   -2   -2    2   -2   -2
NA     NA     NA     NA     NA    2   -2    2   -2   -2
NA     NA     NA     NA     NA   -2    2    2   -2   -2
....
```

```
+3

source to share

This approach does the trick:

``````lapply(split(expmat, row(expmat)), function(u){
cbind(matrix(rep(u,nrow(mat)), ncol=ncol(mat), byrow=T), mat)
})
```

```

`split`

Will basically compile a list of strings `expmat`

, then for each row you duplicate it 7 times and concatenate with a matrix `mat`

. This will give you the 8 matrices you want.

+1

source

Adjust the loop for each line `expmat`

, `cbind`

by aiming it at `mat`

:

``````lapply(1:nrow(expmat), function(x) cbind(expmat[rep(x,nrow(mat)),],mat) )
```

```

`rep`

requires one line to `expmat`

repeat for each line `mat`

to which it is appended. For example, with a very simple example:

``````expmat <- matrix(1:4,nrow=2)
mat    <- matrix(5:8,nrow=2)
lapply(1:nrow(expmat), function(x) cbind(expmat[rep(x,nrow(mat)),],mat) )

#[]
#     [,1] [,2] [,3] [,4]
#[1,]    1    3    5    7
#[2,]    1    3    6    8
#
#[]
#     [,1] [,2] [,3] [,4]
#[1,]    2    4    5    7
#[2,]    2    4    6    8
```

```
0

source

All Articles