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

2014-08-14 Thread Adrian Johnson
Hi Bill, sorry for trouble. It did not work both solutions. Error in `[-`(`*tmp*`, i, value = 1) : subscript out of bounds my x matrix is may not have items that x1 has. say x only has A,B, C, D , whereas x1 has K, L, M , A and D. However x1 does not have any relationship between B and C

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

2014-08-14 Thread William Dunlap
This is what I got: x1 - data.frame(V1=c(K,D,K,M), V2=c(L,A,M,A)) X - array(0, c(4,4), rep(list(LETTERS[1:4]), 2)) f(X, x1, badEntryAction=omitRows) A B C D A 0 0 0 0 B 0 0 0 0 C 0 0 0 0 D 1 0 0 0 table(lapply(x1, factor, levels=LETTERS[1:4])) V2 V1 A B C D A 0 0 0 0 B 0 0 0 0 C 0

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

2014-08-13 Thread John McKown
On Tue, Aug 12, 2014 at 7:14 PM, Adrian Johnson oriolebaltim...@gmail.com wrote: Hi: sorry I have a basic question. I have a data frame with two columns: x1 V1 V2 1 AKT3TCL1A 2 AKTIPVPS41 3 AKTIPPDPK1 4 AKTIP GTF3C1 5 AKTIPHOOK2 6 AKTIPPOLA2

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

2014-08-13 Thread Adrian Johnson
Hi. Thank you for your help. yes, thats exactly right - but the 1211x1211 matrix has some row/column elements that may not be present in x1. Is that the reason I get this error? My matrix row names and column names are identical. I changed the order in my dput code for representational purpose so

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

2014-08-13 Thread William Dunlap
You can replace the loop for (i in nrow(x1)) { x[x1$V1[i], x1$V2[i]] - 1; } by f - function(x, x1) { i - as.matrix(x1[, c(V1,V2)]) # 2-column matrix to use as a subscript x[ i ] - 1 x } f(x, x1) You will get an error if not all the strings in the subscript matrix are in the row or

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

2014-08-13 Thread Adrian Johnson
Hello again. sorry for question again. may be I was not clear in asking before. I don't want to remove rows from matrix, since row names and column names are identical in matrix. I tried your suggestion and here is what I get: fx - function(x,x1){ + i - as.matrix(x1[,c(V1,V2)]) + x[i]-1 + x

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

2014-08-13 Thread William Dunlap
I may have missed something, but I didn't see the result you want for your example. Also, none of the entries in the x1 you showed are row or column names in x, making it hard to show what you want to happen. Here is a function that gives you the choice of *error: stop if any row of x1 is

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

2014-08-13 Thread William Dunlap
Another solution is to use table to generate your x matrix, instead of trying to make one and adding to it. If you want the table to have the same dimnames on both sides, make factors out of the columns of x1 with the same factor levels in both. E.g., using a *small* example: X1 -

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

2014-08-12 Thread Adrian Johnson
Hi: sorry I have a basic question. I have a data frame with two columns: x1 V1 V2 1 AKT3TCL1A 2 AKTIPVPS41 3 AKTIPPDPK1 4 AKTIP GTF3C1 5 AKTIPHOOK2 6 AKTIPPOLA2 7 AKTIP KIAA1377 8 AKTIP FAM160A2 9 AKTIPVPS16 10 AKTIPVPS18 I have a matrix

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

2014-08-12 Thread arun
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.