Monday, 15 February 2010

How to create a distance matrix using a loop in R -



How to create a distance matrix using a loop in R -

i have many samples (in case, trees (targets , neighbors)) in many different sites location (idplot_2012). want create distance matrix each target tree row , distance of each neighbors of site in different column. tried:

neighdist<-function(targetx, targety, neighborx, neighbory) { sqrt((targetx-neighborx)^2 + (targety-neighbory)^2) } distances <- matrix(0, nrow=nrow(targets), ncol=max.neighbors) neighbors.for.tree <- 0 (j in 1:nrow(targets)) { neighbors.for.tree <- subset(neighbors, idplot_2012==j) if (nrow(neighbors.for.tree) > 0) { distances[j,1:nrow(neighbors.for.tree)] <- neighdist(targets$positionx[j], targets$positiony[j], neighbors.for.tree$positionx, neighbors.for.tree$positiony) } }

however, there's bug somewhere. can help me ?

r

No comments:

Post a Comment