[R] Question on output of vectors from a for loop into a matrix

Ryan Briscoe Runquist rdbriscoe at ucdavis.edu
Fri Aug 17 00:35:22 CEST 2007


Hello R-help,

I am a recent convert to R from SAS and I am having some difficulty with
output of a for loop into a matrix.  I have searched the help manuals and
the archives, but I can't get my code to work.  It is probably a syntax
error that I am not spotting.  I am trying to make a distance matrix by
extracting the distances from each point to all others in a vector (the for
loop).  I can get them all to print and can save each individually to a
file, but I cannot get them to be bound into a matrix.

Here is the code that I have tried:

m<-length(Day.1.flower.coords$x) #31 grid points

output.matix<-matrix(0.0,m,m)
for(i in 1:m){
		dist<-spDistsN1(Day.1.coords.matrix, Day.1.coords.matrix[i,])
		dist<-as.vector(dist)
		output.matrix[i,]<-dist
		print(dist)} 

The error message that I get is:

Error in output.matrix[i,] <- dist : incorrect number of subscripts on matrix

Thanks for your help.

Ryan

~~~~~~~~~~~~~~~~~~
Ryan D. Briscoe Runquist
Population Biology Graduate Group
University of California, Davis
rdbriscoe at ucdavis.edu



More information about the R-help mailing list