[R] problem with nested loops

Jens Oldeland oldeland at gmx.de
Wed Jul 30 09:49:03 CEST 2008

Dear all,

I have a problem with constructing a nested loop.

I have two matrices:

800 rows 14 columns

where rows are 40x20 meaning that 40 rows belong to one of twenty 
objects in the matrix pnr

20 rows and 14 columns

I want to:
calculate a distance value with the first 40 rows of pnr but 20times 
with the 1st row of mvp

then the next 40 rows of pnr and 20times with the second row of mvp and 
so on. in total 20 times.

i tried with:

 > dim(p)
[1] 800  14
 > dim(mvp)
[1] 20 14

specdist <-matrix(NA,nrow=40,ncol=20)

for (j in 1:40){
   for(i in 1:20){

but it only repeats from 1:40 and does not step from 41 to 80; from 81 
to 40 etc. certainly there is a "if" "else" "next" "step??" or "while" 
missing, but I never implemented one of those. :(

I would be very grateful for any help!
thank you


More information about the R-help mailing list