I have two dataframes, both of them have three columns, name, latitude and longitude. I want to find the distances between them. I wrote a function called distance
with four parameters (lat1, long1, lat2, long2)
. That is working well. I want to append everything into a dataframe. I wrote the following function,
map <- function (p,k){
for(i in 1:nrow(p)){
q = 0
data = data.frame(w= numeric(0), e = numeric(0), z = character(0))
for(j in 1:nrow(k)){
data$e[q] = p$name[i]
data$z[q] = k$cellname[j]
data$w[q] = earthDist(p$lon[i],p$lat[i], k$longitude[j], k$latitude[j]) / 1.6
q =q + 1
}
}
}
But this is throwing error,
" replacement has 1 row, data has 0 "
and warning,
"invalid factor level, NA generated".
I want to create a dataframe such as,
Data1 Data2 Distance
A1 A2 2.0
A1 B2 3.5
A1 C2 5.0
.
.
.
.
Can anybody help me in creating such a dataframe or tell what is the mistake here.
Thanks