Ryan Briscoe Runquist
2007-Aug-16 22:35 UTC
[R] Question on output of vectors from a for loop into a matrix
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
Dylan Beaudette
2007-Aug-17 01:15 UTC
[R] Question on output of vectors from a for loop into a matrix
On Thursday 16 August 2007 15:35, Ryan Briscoe Runquist wrote:> 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 >Hi Bryan, for all UCD students you have the luxury of not using a loop! :) Would something like this work - for the creation of a 'distance matrix' : # example dataset: 2 x 2 grid d <- expand.grid(x=1:2, y=1:2) # an instructive plot plot(d, type='n') text(d, rownames(d)) # create distance object and convert to matrix: m <- as.matrix(dist(d)) m 1 2 3 4 1 0.000000 1.000000 1.000000 1.414214 2 1.000000 0.000000 1.414214 1.000000 3 1.000000 1.414214 0.000000 1.000000 4 1.414214 1.000000 1.000000 0.000000 # is that the kind of distance matrix you are looking for : ?dist Distance Matrix Computation Description: This function computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix. [...] cheers, Dylan> ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Dylan Beaudette Soils and Biogeochemistry Graduate Group University of California at Davis 530.754.7341