1

I have a list of latitudes and longitudes (each row contains a labeling number, 2 points, which form a line between them), and I am trying to find the distance from one of the endpoints in each row to the line created by every other row in the list. I am using the geosphere(dist2gc) package and dplyr. I am trying to do it through looping (because I cannot figure out how to get lapply to do it), and I want to create a new column with the distances for each comparison (so if I start with 10 rows, I will compare 10 times 10 and have 10 new columns with the distances to the lines in each row). The output if I start with a 10x8 data.frame would be a 10x18 data.frame.

The temporary column name is not working. Please help!

Can anyone help me come up with the way to make variable naming work, or other approaches to do this?

dist <- function(df){
 idx <- seq(1, nrow(df)) # create index values
 namelist <- df[,2] # extract name list for new columns
  for (i in idx){
   templon <- df[i,3] # pull comparison value point for lat and lon
   templat <- df[i,4]
   tempcoln <- namelist[i]
   df <- mutate(df, !! tempcoln := dist2gc(cbind(Longitude1, Latitude1), 
     cbind(Longitude2, Latitude2), cbind(templon, templat), r=radius, 
     sign=FALSE))
  }
  return(df)
}
  • Please add some data so that we can reproduce the problem. – erc Jul 12 '18 at 07:20
  • ID number Longitude_1 Latitude_1 Longitude_2 Latitude_2 1 -102.8172836 48.31227875 -102.8139877 48.2853508 2 -103.4087677 48.15821075 -103.3791809 48.15842056 3 -103.6404572 48.40116882 -103.6405334 48.37358856 4 -103.4186172 48.22603989 -103.4222107 48.1999588 5 -103.1802826 47.70434189 -103.1827087 47.73215103 6 -102.5798187 47.50204086 -102.5781097 47.47267151 7 -103.4195175 47.93582916 -103.4218063 47.96218872 8 -103.6446762 48.16933823 -103.6390686 48.19701004 9 -103.1062317 48.74779129 -103.1062622 48.72108078 – Alex C Fleming Jul 12 '18 at 13:08

0 Answers0