envdist = read.csv("EnvDist-Input.csv") testdist = subset(envdist, Country == "ISR" | Country == "FJI") distance <- matrix(data = 0, nrow = nrow(testdist), ncol = nrow(testdist) ) rownames(distance) <- testdist$PortName colnames(distance) <- testdist$PortName for (i in 1:nrow(testdist)) { for (j in i+1:nrow(testdist)-i) { # d(a,b) = sqrt( (a1-b1)^2 + (a2-b2)^2 + (a3-b3)^2 + (a4-b4)^2 ) distance[i, j] <- sqrt( (testdist[i,5] - testdist[j,5])^2 + (testdist[i,6] - testdist[j,6])^2 + (testdist[i,7] - testdist[j,7])^2 + (testdist[i,8] - testdist[j,8])^2 ) } } print(distance)
It does some fancy math to four columns of numbers and puts the result into a matrix.
The general point of this exercise is to compare environmental similarities between each and every port in the world, based on temperature and salinity. There are over 6600 ports to compare between, so when I do the real run, the matrix will be very large... but for starters, I just needed to make a functional FOR loop in R.
No comments:
Post a Comment