Search code examples
rmatrixfill

How fill a matrix with 1 and 0 when there is association


I´ve been trying to make a matrix from a data frame in R, without succes. I have the next data frame

Order        Object      idrA   idoA
8001505892  CHR56029398AB   1   1
8001506013  CHR56029398AB   1   2
8001507782  CHR56029398AB   1   3
8001508088  CHR56029398AB   1   4
8001508788  CHR56029398AB   1   5
8001509281  CHR56029398AB   1   6
8001509322  CHR56029398AB   1   7
8001509373  CHR56029398AB   1   8
8001505342  MMRMD343563     2   9
8001506699  MMRMD343563     2   10
8001507102  MMRMD343563     2   11
8001507193  MMRMD343563     2   12
8001508554  MMRMD343563     2   13
8001508654  MMRMD343563     2   14
8001509151  MMRMD343563     2   15
8001509707  MMRMD343563     2   16
8001509712  MMRMD343563     2   17
8001509977  MMRMD343563     2   18
8001510279  MMRMD343563     2   19
8001505342  MMRMD343565     3   9
8001507112  MMRMD343565     3   20
8001507193  MMRMD343565     3   12
8001508554  MMRMD343565     3   13
8001508654  MMRMD343565     3   14
8001509151  MMRMD343565     3   15
8001509707  MMRMD343565     3   16
8001509712  MMRMD343565     3   17
8001509977  MMRMD343565     3   18
8001510279  MMRMD343565     3   19
8001505920  MMRMN146319     4   21
8001506733  MMRMN146319     4   22
8001506929  MMRMN146319     4   23
8001507112  MMRMN146319     4   20
8001507196  MMRMN146319     4   24
8001510302  MMRMN146319     4   25
8001517272  MMRMN146319     4   26
8001506186  MMRMN146320     5   27
8001506733  MMRMN146320     5   22
8001506929  MMRMN146320     5   23
8001507112  MMRMN146320     5   20
8001508638  MMRMN146320     5   28
8001509526  MMRMN146320     5   29
8001505452  SSR664050011    6   30
8001508551  SSR664050011    6   31
8001509229  SSR664050011    6   32
8001510174  SSR664050011    6   33

Where idr are the Id for each object and ido is the Id for each purchase order. So I want to make a matriz with the number of row = N° orders and N° columns= N°object, and fill it with a vector with 1s and 0s, with a 1 when in each order was purchased some of the bjects and 0 if it wasn´t.

Example: the order with ido=20 must have a vector like this (0,0,1,1,1,0).

I hope I could explain clearly, thanks!


Solution

  • You can use xtabs to create a cross table:

    Recreate your data:

    dat <- read.table(header=TRUE, text="
                      Order        Object      idrA   idoA
                      8001505892  CHR56029398AB   1   1
                      ....
                      8001506013  CHR56029398AB   1   2
                      8001507782  CHR56029398AB   1   3
                      8001509229  SSR664050011    6   32
                      8001510174  SSR664050011    6   33")
    

    Create the cross table:

    xtabs(Order ~ idoA + idrA, dat) != 0
    
        idrA
    idoA     1     2     3     4     5     6
      1   TRUE FALSE FALSE FALSE FALSE FALSE
      2   TRUE FALSE FALSE FALSE FALSE FALSE
      ....
      20 FALSE FALSE  TRUE  TRUE  TRUE FALSE
      ....
      32 FALSE FALSE FALSE FALSE FALSE  TRUE
      33 FALSE FALSE FALSE FALSE FALSE  TRUE
    

    To coerce the logical values to numeric values, you can use apply() and as.numeric, but then you have some work left to replace the row names:

     apply(xtabs(Order ~ idoA + idrA, dat) != 0, 2, as.numeric)
    

    Or, you can use a little trick by adding 0 to the values. This coerces the logical values to numeric:

     (xtabs(Order ~ idoA + idrA, dat) != 0) + 0
    
        idrA
    idoA 1 2 3 4 5 6
      1  1 0 0 0 0 0
      2  1 0 0 0 0 0
      3  1 0 0 0 0 0
    ....