Creating a maxtrix from "conditional prints"
Use rbind? Not the most optimal solution, but it should get the job done.
# not tested
Code example:
out <- c()
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)
out <- rbind(out,qua)
}}
On Fri, Jun 4, 2010 at 9:08 PM, EM <evilmastea at gmail.com> wrote:
Hi guys :)
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)
Thanks for your help, in advance :)
? ? ? ?[[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.
Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php