[R] help, please! matrix operations inside 3 nested loops

Petr PIKAL petr.pikal at precheza.cz
Thu Aug 9 14:46:32 CEST 2012


Hi

> all problems solved. thank you for your help!
> for the sake of completeness, here my solution:
> #1) read in data:
> daten<-read.table('K:/Analysen/STRUCTURE/test.txt', header=TRUE, 
sep="\t")
> daten<-as.data.frame(daten)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
not needed, daten is already data frame

> 
> #2) create empty matrix:
> indxind<-matrix(0,nrow=617, ncol=617) 
> #indxind[1:20,1:19]
> 
> #3) compare cells to each other, score:
> #for the whole dataset: s in 3:34, z1 in 1:617, z2 in 1:617
> z1<-1 #running variable for rows in daten
> z2<-1 #running variable for rows in daten
> l1<-1 #running variable for rows in indxind
> l2<-1 #running variable for rows in indxind
> for (s in 3:6) {   #walks though the matrix colum by colum, starting at
> colum 3
>                 while (z1<11) {  #for each current colum, take one row
> (z1)...
>                                 while (z2<11) {  #...and compare it to
> another row (z2) of the current colum
>                                               if (z1!=z2) {
>                                                           l1
> 
> topf<-indxind[l1,l2]
>                                                           if
> (daten[z1,s]==daten[z2,s]) topf<-topf+1   #actually, 2 rows make up 1
> individual,
>                                                           if
> (daten[z1,s]==daten[z2+1,s]) topf<-topf+1      #therefore i compare 2 
rows
>                                                           if
> (daten[z1+1,s]==daten[z2,s]) topf<-topf+1      #with another 2 rows
>                                                           if
> (daten[z1+1,s]==daten[z2+1,s]) topf<-topf+1
> 
> indxind[l1,l2]<-topf
>                                                           }
>                                               z2<-z2+2
>                                               l2<-l2+1
>                                               }
>                                 z2<-1
>                                 l2<-1
>                                 z1<-z1+2
>                                 l1<-l1+1
>                               }
>                 z1<-1
>                 l1<-1
>                }
> 
> #4) check:
> indxind[1:5,1:5]

I believe that above cycles can be simplified, maybe by changing your 
daten to three dimensional array or some clever **ply construction but if 
your loops works it is not probably worth en effort.

Regards
Petr

> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/help-please-
> matrix-operations-inside-3-nested-loops-tp4639592p4639744.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list