merging two matrices row wise

I have two matrices A and B, where matrix A of order 26124 and matrix B of order 10424, I want to merge matrix B in A in such a way that rows of B [1,2,3,4,5,...,104] are merge in following row position in A as [5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62,68,69,75,76,82,83,89,90,96,97,....]
so that matrix A becomes of order 365*24. How I could do this in R?

Hi there,

Happy to help if you can make a reprex (FAQ: How to do a minimal reproducible example ( reprex ) for beginners) . Go ahead and make a smaller version of the two matrices as well as the solution will probably be able to generalise for your specific requirement.

thanks, Grey Merchant for your suggestion.

Hi @faheem

I went with the approach to add an index to each row of the two matrices and arrange them according to that index.

#create example data
A <- matrix(sample(20:50, 261*24, replace = TRUE), ncol = 24)
B <- matrix(sample(20:50, 104*24, replace = TRUE), ncol = 24)

Then convert each matrix to a tibble, add an index column.

library(tidyverse)
A_with_index <- A %>% as_tibble() %>% 
  mutate(
    cuttingpoint = row_number() %/% 5, 
    index = row_number() + 2* cuttingpoint
  )

# A tibble: 261 x 11
#>      V16   V17   V18   V19   V20   V21   V22   V23   V24 cuttingpoint index
#>    <int> <int> <int> <int> <int> <int> <int> <int> <int>        <dbl> <dbl>
#>  1    41    50    33    37    49    20    35    38    35            0     1
#>  2    34    32    29    35    43    24    48    49    48            0     2
#>  3    36    35    45    44    35    23    22    48    30            0     3
#>  4    23    41    23    48    42    24    25    41    50            0     4
#>  5    46    22    30    28    47    44    43    49    42            1     7
#>  6    29    28    47    23    46    21    28    39    42            1     8
#>  7    26    50    43    38    31    34    41    32    43            1     9
#>  8    35    36    30    40    35    46    30    24    44            1    10
#>  9    48    29    36    41    34    39    48    23    47            1    11
#> 10    28    36    29    28    41    37    40    26    21            2    14
#> 11    39    28    50    50    31    50    20    38    30            2    15

B_with_index <- B %>% as_tibble() %>% 
  mutate(
    cuttingpoint = row_number() %/% 2 %>% lag(default = 0), 
    index = row_number() + 5 * cuttingpoint + 4
  )

# A tibble: 104 x 11
#>      V16   V17   V18   V19   V20   V21   V22   V23   V24 cuttingpoint index
#>    <int> <int> <int> <int> <int> <int> <int> <int> <int>        <dbl> <dbl>
#>  1    30    40    20    43    38    38    34    32    27            0     5
#>  2    23    29    20    40    46    33    47    46    34            0     6
#>  3    40    30    29    30    30    36    33    40    48            1    12
#>  4    40    23    31    23    34    22    31    47    26            1    13
#>  5    28    47    36    36    40    49    22    49    44            2    19
#>  6    34    37    27    41    31    27    43    28    28            2    20
#>  7    39    20    48    25    46    37    36    34    22            3    26

And then your result, back to a matrix:

A_plus_B <- bind_rows(A_with_index, B_with_index) %>% arrange(index) %>% select(-cuttingpoint, -index) %>% as.matrix()

Hope it helps.

yes it help but I do this in a very simple way

A<- matrix(2:21, nrow = 40, ncol=4)
B <- matrix(2*(1:32),nrow = 8, ncol=4)
R <- matrix(0, nrow(A) + nrow(B), ncol(A))
i <- 5:6 + rep(7L * 0:(nrow(B)/2-1), each = 2L)
R[i, ] <- B
R[-i, ] <- A

I think this more simple

1 Like

This topic was automatically closed 7 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.