[R] populating matrix with binary variable after matching data from data frame

John McKown john.archie.mckown at gmail.com
Wed Aug 13 14:28:06 CEST 2014


On Tue, Aug 12, 2014 at 7:14 PM, Adrian Johnson <oriolebaltimore at gmail.com>
wrote:

> Hi:
> sorry I have a basic question.
>
> I have a data frame with two columns:
> > x1
>       V1       V2
> 1   AKT3    TCL1A
> 2  AKTIP    VPS41
> 3  AKTIP    PDPK1
> 4  AKTIP   GTF3C1
> 5  AKTIP    HOOK2
> 6  AKTIP    POLA2
> 7  AKTIP KIAA1377
> 8  AKTIP FAM160A2
> 9  AKTIP    VPS16
> 10 AKTIP    VPS18
>
>
> I have a matrix 1211x1211 (using some elements in x1$V1 and some from
> x1$V2). I want to populate for every match for example AKT3 = TCL1A = 1
> whereas AKT3 - VPS41 gets 0)
> How can i map this binary relations in x.
>
>
> >x
>        TCLA1 VPS41 ABCA13 ABCA4
> AKT3       0     0      0     0
> AKTIP      0     0      0     0
> ABCA13     0     0      0     0
> ABCA4      0     0      0     0
>
>
<snip>

I'm not totally sure that I understand your data structure. So I will
rephrase a bit so that I can be corrected, if necessary. You have an
1211x1121 matrix already. Every cell in the matrix is initialized to 0. It
has column names such as TCLA1, VPS41, ABCA13, ABCA4, ... and it has row
names such as AKT3 AKTPI, ABCA13, ABCA4. The list "x1" has columns named V1
and V2. V1 values are row names in the matrix. V2 values are column names
in the matrix. The following should do what you want. It is not a _good_
solution because it is iterative. But it is a start

for (i in nrow(x1)) {
   x[x1$V1[i], x1$V2[i]] <- 1;
}


>
> Thanks
> Adrian
>
>         [[alternative HTML version deleted]]
>
>
Please post in plain text, per the mailing list "rules".

-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]



More information about the R-help mailing list