rbinary-matrix

How do I make a binary matrix comparing several columns


I'm new to programming and R.

I have a data like this in columns:

C1        C2     C3        C4          C5
Apple            Apple     Banana      Banana
Banana           Orange    Orange
Orange

I want to make a binary matrix comparing all the columns to C1 where 1 is TRUE and 0 is FALSE. I want something like this:

 C1        C2     C3        C4          C5
Apple      0      1         0           0
Banana     0      0         1           1
Orange     0      1         1           0

Does anyone know how to do this? Thank you.


Solution

  • You can loop over C2-C4 and match the elements to C1, i.e.

    (!is.na(sapply(dd[-1], function(i)match(dd$C1, i))))*1
    
    #     C2 C3 C4 C5
    #[1,]  0  1  0  0
    #[2,]  0  0  1  1
    #[3,]  0  1  1  0
    

    Or bind them together with C1, i.e.

    cbind.data.frame(C1 = dd$C1, (!is.na(sapply(dd[-1], function(i) match(dd$C1, i)))) * 1)
    
    #      C1 C2 C3 C4 C5
    #1  Apple  0  1  0  0
    #2 Banana  0  0  1  1
    #3 Orange  0  1  1  0