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)
}