Calculate distance between each tag number in R

127 views Asked by At

I am trying to calculate the Euclidean distance between each tag number of trees in R. My data set has approximately 43000 rows. I am using

mat.co <- cbind(co.trees$gx, co.trees$gy)
m <- as.matrix(head(dist(mat.co, diag = TRUE, upper = TRUE)))
as.dist(m, diag = TRUE, upper = TRUE)

My problems are:

1) the matrix wouldn't produce the column and row names that I want. Right now the column and row names are 1,2,3, etc. I want both the column and row names to be the tag numbers of the trees, so that I can find out the relative distance.

Also, the matrix does not display the distance between 1 and 1 , which is supposed to be 0. This also caused a problem later in the latter part, where I used as.dist.

2) when I applied as.dist, the distance values don't match up with the corresponding tag numbers. Now, in addition to the 0 value that didn't show up before, the next value is missing too.

I used head in the codeline #2 because I got an error message of reaching the memory limit of R. I am new to R; any detailed explanation is very much appreciated!! Thank you very much!

1

There are 1 answers

0
cye On

I realized the following lines of codes can work to produce the distance values with the tag numbers I want.

n <- dist(head(cbind(co.trees$gx, co.trees$gy)))

n <- as.matrix(n)

colnames(n) <- head(co.trees$tag)

rownames(n) <- head(co.trees$tag)