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

Petr PIKAL petr.pikal at precheza.cz
Wed Aug 8 17:30:47 CEST 2012


Hi

> 
> hello, this is my script:
> 
> #1) read in data:
> 
daten<-read.table('K:/Analysen/STRUCTURE/input_STRUCTURE_tab_excl_5_282_559.txt',
> header=TRUE, sep="\t")
> daten<-as.matrix(daten)

If there is any column with nonnumeric values it will transfer all numeric 
values from daten data.frame to character values.

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

The above code is rather clumsy and it is difficult to understand what it 
shall do without extensive study. AFAIU you first set topf to NA and then 
try to add 1 to topf. The result is again NA regardless of your 
sophisticated z constuction. Therefore you are just computing NA in each 
cycle, so you can not expect other result them NA.


>       #print(c(s,z1,z2,indxind[1,2])) ##counts s, z1 and z2 properly, 
but
> gives NA for indxind[1,2]
>       }
>     #indxind[1:5,1:5] #empty matrix
>   }
>   #indxind[1:5,1:5] #empty matrix
>   }
> 
> #4) check:
> indxind[1:5,1:5]
> 
> this results no errors, but my matrix indxind remains empty (only NAs).
> though all columns and rows are counted properly. R needs quite a while 
to
> get through all this (there are probably smarter and faster ways to
> calculate this but i am not too deep into R and bioinformatics, and i 
need
> to calculate this only once). could the 3 for-loops already be too

What is this. Please try to set up small example with what do you have and 
what do you want to achive. Unless you can explain better what do you 
want, you probably will not get better answer. 

I, however, may be proven wrong as some clever people in this list are far 
better in mind reading then I am :-)

Regards
Petr


> computationally intense for adding matrix operations?
> 
> any help would be much appreciated!
> 
> thx, frido
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/help-please-
> matrix-operations-inside-3-nested-loops-tp4639592.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