Adrian Johnson
2014-Aug-13 00:14 UTC
[R] populating matrix with binary variable after matching data from data frame
Hi: sorry I have a basic question. I have a data frame with two columns:> x1V1 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.>xTCLA1 VPS41 ABCA13 ABCA4 AKT3 0 0 0 0 AKTIP 0 0 0 0 ABCA13 0 0 0 0 ABCA4 0 0 0 0 dput - x = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim c(4L, 4L), .Dimnames = list(c("AKT3", "AKTIP", "ABCA13", "ABCA4" ), c("TCLA1", "VPS41", "ABCA13", "ABCA4"))) x1 = structure(list(V1 = c("AKT3", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP"), V2 = c("TCL1A", "VPS41", "PDPK1", "GTF3C1", "HOOK2", "POLA2", "KIAA1377", "FAM160A2", "VPS16", "VPS18")), .Names = c("V1", "V2"), row.names = c(NA, 10L), class = "data.frame") Thanks Adrian [[alternative HTML version deleted]]
arun
2014-Aug-13 03:27 UTC
[R] populating matrix with binary variable after matching data from data frame
You could try: x1$V2[1] <- "TCLA1" ? x[outer(rownames(x), colnames(x), FUN=paste) %in% as.character(interaction(x1, sep=" "))] <- 1 x ?????? TCLA1 VPS41 ABCA13 ABCA4 AKT3?????? 1???? 0????? 0???? 0 AKTIP????? 0???? 1????? 0???? 0 ABCA13???? 0???? 0????? 0???? 0 ABCA4????? 0???? 0????? 0???? 0 A.K. On Tuesday, August 12, 2014 8:16 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 dput - x = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim c(4L, 4L), .Dimnames = list(c("AKT3", "AKTIP", "ABCA13", "ABCA4" ), c("TCLA1", "VPS41", "ABCA13", "ABCA4"))) x1 = structure(list(V1 = c("AKT3", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP", "AKTIP"), V2 = c("TCL1A", "VPS41", "PDPK1", "GTF3C1", "HOOK2", "POLA2", "KIAA1377", "FAM160A2", "VPS16", "VPS18")), .Names = c("V1", "V2"), row.names = c(NA, 10L), class = "data.frame") Thanks Adrian ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
John McKown
2014-Aug-13 12:28 UTC
[R] populating matrix with binary variable after matching data from data frame
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]]