2

These are my two text matrices:

A <- matrix(c("AIP-A","CSV-A"), ncol = 1, byrow = TRUE)
B <- matrix(c("AIP-B","CSV-B"), ncol = 1, byrow = TRUE)

I am trying to get the multiplication of these matrices and the output should look like this:

AIP-A,AIP-B
AIP-A,CSV-B
CSV-A,AIP-B
CSV-A,CSV-B

A*B does not work because it is looking for numeric inputs. Maybe the same result can be achieved by some other technique. I am ok with 4x2 output matrix, a 4x1 matrix ,a character vector, data.frame and data.table

dww
  • 30,425
  • 5
  • 68
  • 111
gibbz00
  • 1,947
  • 1
  • 19
  • 31
  • @Jota absolutely. I did not know of this outer function. Kindly post it as an answer. Thank you! – gibbz00 Jul 15 '16 at 14:54
  • 2
    Possible duplicate of [Pasting two vectors with combinations of all vectors' elements](http://stackoverflow.com/questions/16143700/pasting-two-vectors-with-combinations-of-all-vectors-elements) – user2100721 Jul 15 '16 at 15:09
  • 1
    Also, I would suggest that you delete the repeated thank you comments. While appreciated, these are generally frowned upon on SO. – G. Grothendieck Jul 15 '16 at 15:28

5 Answers5

4

You can use outer for outer product of arrays with paste to get your desired output:

c(outer(A, B, paste, sep = ","))
# [1] "AIP-A,AIP-B" "CSV-A,AIP-B" "AIP-A,CSV-B" "CSV-A,CSV-B"

or

matrix(outer(A, B, paste, sep = ","), ncol = 1)
#     [,1]         
#[1,] "AIP-A,AIP-B"
#[2,] "CSV-A,AIP-B"
#[3,] "AIP-A,CSV-B"
#[4,] "CSV-A,CSV-B"
Jota
  • 17,281
  • 7
  • 63
  • 93
2

Not sure about your definition of "multiplication" but using cbind and rep you get this:

> cbind(rep(A,each=nrow(B)),rep(B,nrow(A)))
     [,1]    [,2]   
[1,] "AIP-A" "AIP-B"
[2,] "AIP-A" "CSV-B"
[3,] "CSV-A" "AIP-B"
[4,] "CSV-A" "CSV-B"
dratewka
  • 2,104
  • 14
  • 15
2

This method uses expand.grid with Reduce and paste:

Reduce(function(...) paste(..., sep=","), expand.grid(A,B))
[1] "AIP-A,AIP-B" "CSV-A,AIP-B" "AIP-A,CSV-B" "CSV-A,CSV-B"
lmo
  • 37,904
  • 9
  • 56
  • 69
2

Here is an option using CJ from data.table

library(data.table)
CJ(A[,1], B[,1])[, paste(V1, V2, sep=", ")]
#[1] "AIP-A, AIP-B" "AIP-A, CSV-B" "CSV-A, AIP-B" "CSV-A, CSV-B"

Wrap it with matrix to create a single column matrix

 matrix(CJ(A[,1], B[,1])[, paste(V1, V2, sep=", ")])
 #   [,1]          
 #[1,] "AIP-A, AIP-B"
 #[2,] "AIP-A, CSV-B"
 #[3,] "CSV-A, AIP-B"
 #[4,] "CSV-A, CSV-B"

Or using dplyr/tidyr

 library(dplyr)
 library(tidyr)
 data_frame(A=A[,1], B=B[,1]) %>% 
         complete(A,B) %>% 
          unite(AB, A,B)
 #          AB
 #        <chr>
 #1 AIP-A_AIP-B
 #2 AIP-A_CSV-B
 #3 CSV-A_AIP-B
 #4 CSV-A_CSV-B
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Assuming that you want a 4x2 output matrix, try expand.grid. No packages are used.

as.matrix(expand.grid(B = B, A = A)[2:1])

giving:

     A       B      
[1,] "AIP-A" "AIP-B"
[2,] "AIP-A" "CSV-B"
[3,] "CSV-A" "AIP-B"
[4,] "CSV-A" "CSV-B"

If the order, whether it is a matrix and the headings are unimportant then expand.grid(A, B) may be sufficient.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341