0

I have two data frames with the first three columns equal, and I want to merge them. One of the data frames is bigger than the other but all the rows are related. I have:

> head(MatrixAuxiliar1)
  position direction[1] direction[2] ker[1] ker[2] ker[3] ker[4]
1        2            2            1     -1      2     -2      1
2        2            2            3     -1     -2      2      1
3        2            2            5     -3     -2      2      3
4        2            2            6     -2     -1      1      2
5        2            2            7     -5     -2      2      5
6        2            2            8     -3     -1      1      3
> dim(MatrixAuxiliar1)
[1] 32  7
> head(MatrixAuxiliar2)
  position direction[1] direction[2] order[1] order[2] order[3] order[4]
1        2            2            1        1        2        3        4
2        2            2            3        1        3        2        4
3        2            2            5        3        1        4        2
4        2            2            6        3        1        4        2
5        2            2            7        3        1        4        2
6        2            2            8        3        1        4        2
> dim(MatrixAuxiliar2)
[1] 300   7

The same $position and $direction always appear one time in MatrixAuxiliar1 but can appear several times (at least one) in MatrixAuxiliar2. I want that, if several rows have the same $position and $direction the resultant data frame had several rows with the same $position, $direction, and $kernel for the first data frame and different orders from the second data frame. The data frame should have 300 rows.

I tried to use the function merge but the output is a data frame with 33 rows.

> merge.data.frame(MatrixAuxiliar2,MatrixAuxiliar1,by=c(1,2,3))
   position direction[1] direction[2] order[1] order[2] order[3] order[4] ker[1] ker[2] ker[3] ker[4]
1         2            2            1        1        2        3        4     -1      2     -2      1
2         2            2            3        1        3        2        4     -1     -2      2      1
3         2            2            5        3        1        4        2     -3     -2      2      1
.
.
.

32        4            9            5        1        2        3        4     -2      7     -7      2
33        4            9            5        4        1        2        3     -2      7     -7      2
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 1
    check out the `all.x =`, `all.y =`, and `all =` arguments in `merge()`. These can help you define a left, right, inner, and outer joins. https://www.datasciencemadesimple.com/join-in-r-merge-in-r/ – Skaqqs Sep 27 '21 at 20:08
  • Based on your description, I'd suggest using `all = TRUE` in `merge()` for a full join. This will keep all rows from both data frames whether or not there is a match in the other data frame. If you find rows in your result with `NA` values for `ker` columns, that means that particular position/dir[1]/dir[2] combination only occurred in `MatrixAuxiliar2`. – Gregor Thomas Sep 27 '21 at 20:26

0 Answers0