# [R] Creating a maxtrix from "conditional prints"

From: EM <evilmastea_at_gmail.com>
Date: Fri, 04 Jun 2010 21:08:22 +0200

I'm dealing with this problem, perhaps conceptually not that complex, but still - I'm stuck.

Two columns, values 1<x<10, only integers. I want to check when the first column's index is identical to the second's (and vice versa). If that's true, I want to add a further column with value 1 (if true) or NA (if false).
Thus, I obtain 100 matrices (for each columns I will have 1-1, 1-2, 1-3 etc). Now, I want R to consider only those matrices whose "new" column has value = 1 & whose total number of rows is equal to 2. I can get R to "print" this result inside the "for" cycle, yet I can't manage to build a single matrix, to store all the results altoghether - which is what I really want.

Code example:

for (x in 1:10) {
for (y in 1:10) {
qui <- ifelse((mac[,1] == x) & (mac[,5] == y) | (mac[,1] == y) & (mac[,5] == x), 1, NA)
quo <- cbind(mac,qui)
qua <- subset(quo, qui ==1)
if(nrow(qua) == 2)
print(qua)
}}

result (wrong, now):

```     ricevente genere_r abo_r classieta_r donatore genere_d abo_d
classieta_d    eta_d mismatch pra comp       mum qui

[1,]         8        0     1           3        9        1     1

4 56.17437        2   1    1 -6.645437   1

[2,]         9        1     1           2        8        0     1

3 48.77579        2   1    1 -5.905579   1
ricevente genere_r abo_r classieta_r donatore genere_d abo_d
classieta_d    eta_d mismatch pra comp       mum qui

[1,]         8        0     1           3       10        0     0

3 48.77579        2   1    1 -5.905579   1

[2,]        10        0     2           5        8        0     1

3 48.77579        1   1    1 -5.391579   1
ricevente genere_r abo_r classieta_r donatore genere_d abo_d
classieta_d    eta_d mismatch pra comp       mum qui

[1,]         8        0     1           3        9        1     1

4 56.17437        2   1    1 -6.645437   1

[2,]         9        1     1           2        8        0     1

3 48.77579        2   1    1 -5.905579   1
ricevente genere_r abo_r classieta_r donatore genere_d abo_d
classieta_d    eta_d mismatch pra comp       mum qui

[1,]         9        1     1           2       10        0     0

3 48.77579        0   1    1 -4.877579   1

[2,]        10        0     2           5        9        1     1

4 56.17437        0   1    1 -5.617437   1

```

what I'd like to get:

```     ricevente genere_r abo_r classieta_r donatore genere_d abo_d
classieta_d    eta_d mismatch pra comp       mum qui

[1,]         8        0     1           3        9        1     1

4 56.17437        2   1    1 -6.645437   1

[2,]         9        1     1           2        8        0     1

3 48.77579        2   1    1 -5.905579   1

[3,]         8        0     1           3       10        0     0

3 48.77579        2   1    1 -5.905579   1

[4,]        10        0     2           5        8        0     1

3 48.77579        1   1    1 -5.391579   1

[5,]         8        0     1           3        9        1     1

4 56.17437        2   1    1 -6.645437   1

[6,]         9        1     1           2        8        0     1

3 48.77579        2   1    1 -5.905579   1

[7,]         9        1     1           2       10        0     0

3 48.77579        0   1    1 -4.877579   1

[8,]        10        0     2           5        9        1     1

4 56.17437        0   1    1 -5.617437   1

```

(don't mind the values & names, this is just a small part of a longer algorithm)

[[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. Received on Fri 04 Jun 2010 - 19:10:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 05 Jun 2010 - 22:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.