demilix.blogg.se

Distance vs increasing x vs closest point alan datagraph
Distance vs increasing x vs closest point alan datagraph




distance vs increasing x vs closest point alan datagraph

Note that you have to include the lat/long columns in the order of first longitude and then latitude.Īlthough this works perfectly on this simple example, in larger datasets where the names are not in the same order, this will lead to problems. The last one is considered the most accurate one (according to the package author). Within this function you can use several formula's for calculating the distance: distCosine, distHaversine, distVincentySphere and distVincentyEllipsoid. Also, the key option of setDT is only currently available in the development version of data.table ( 1.9.5+) with the CRAN version, use setkey(setDT(A),Name), etc.įor calculating the distance between lat/long points, you can use the distm function from the geosphere package. *It may not be strictly necessary to set the key of B, but I think it is good practice to do so. We've used the convenient setDT function to convert A and B (in-line) to a data.table by reference, simultaneously declaring the key to be Name for both*. To start from scratch if A and B are already stored as ames, it's not much more complicated: setDT(A,key="Name") If you don't want to store the distance, and just want the output, you could do: A. The distance you seek is now stored in the data.table A under the column distance. Now, we want to merge A and B by Name (to merge, we need a key set, which I've conveniently done already), then use the respective x and y coordinates to calculate (Euclidean) distance. I'm going to create two data.tables that match your general description first: library(data.table)Ī<-data.table(Name=1:10,x=rnorm(10),key="Name")ī<-data.table(Name=1:10,y=rnorm(10),key="Name") Check out the Getting Started vignettes for more on the package. I like data.table and the syntax here will look very simple. Without a reproducible example, all I can do is offer you a general solution. Any help with that would be appreciated as well. I should also note that I will not be able to do a straightforward euclidean distance because the points occur in water and the path distance between them needs to be in the water (or bounded by some area). Now I want to relate A and B so that I can ask the ultimate question if A$Name=B$Name what is the distance between them using their corresponding lat long data. I envision the final function being something like if A$Name=B$Name then use their corresponding lat/long data to calculate the distance between them.Įxample data: A <- ame(Lat=1:4,Long=1:4,Name=c("a","b","c","d"))ī <- ame(Lat=5:8,Long=5:8,Name=c("a","b","c","d")) Thats why I want to calculate the distance between them. Note that for a given Name the lat/long information is different in each dataframe. For example there is dataframe A and B which both have lat/long information in them and they share the column Name. I figure the first step is to join or relate the data in the two data frames. I am trying to find the distance between points in two different data frames given that they have the same value in one of their columns.






Distance vs increasing x vs closest point alan datagraph