我試圖計算從我的每個25000個觀測值到使用其經度和緯度的50個美國城市的距離。最終,我想獲得每個觀測點到一個主要城市的最小距離,並確定該城市。R在生成變量中用於雙循環
我在這個循環的問題是specifc到 DIST。{$ citygdpcoord城市[J]}在第二循環中,我該如何使它指向由第一回路中生成的dataframes?
for(j in 1:50)
{
assign(paste("dist",citygdpcoord$city[j],sep="."), 0)
for(i in 1:25509)
{
haver <- sin((citygdpcoord$latitude[j]-geofinlit$latitude[i])/2)*
sin((citygdpcoord$latitude[j]-geofinlit$latitude[i])/2)+
cos(geofinlit$latitude[i])*cos(citygdpcoord$latitude[j])*
sin(citygdpcoord$longitude[j]-geofinlit$longitude[i])*sin(citygdpcoord$longitude[j]-geofinlit$longitude[i])
dist.{citygdpcoord$city[j]}<- data.frame(6371.393*2*atan2(sqrt(haver),sqrt(1-haver)))
}
}
也許你可以解釋你試圖實現的目標 – MLavoie
爲什麼在最後一行有'citygdpcoord $ city [j]'的括號?我以前從來沒有見過這樣的人,所以如果那是真正的語法,請原諒我的無知,但對我來說,它看起來像不恰當的語法。 – brittenb
我想你想要get()函數。比如'get(paste('dist',citygdpcoord $ city [j],sep =「。」))' – Jason