R函数的快速求解

R函数的快速求解,r,gps,distance,heatmap,R,Gps,Distance,Heatmap,我正在尝试创建一个“比赛中的球员热度图”。我有gpx格式的gps数据,并且能够将文件上传到R中。数据如下: lon lat ele time position sat 1 20.84293 15.82110 23.9 2016-03-28T17:28:19.407Z 1 3 2 20.84315 15.82129 23.9 2016-03-28T17:28:20.407Z 2 3 3

我正在尝试创建一个“比赛中的球员热度图”。我有gpx格式的gps数据,并且能够将文件上传到R中。数据如下:

         lon      lat  ele                     time position sat
1  20.84293 15.82110 23.9 2016-03-28T17:28:19.407Z        1   3
2  20.84315 15.82129 23.9 2016-03-28T17:28:20.407Z        2   3
3  20.84309 15.82118 23.9 2016-03-28T17:28:22.407Z        3   3
4  20.84269 15.82089 23.9 2016-03-28T17:28:23.407Z        4   3
5  20.84277 15.82092 23.9 2016-03-28T17:28:24.407Z        5   3
6  20.84289 15.82102 23.9 2016-03-28T17:28:25.411Z        6   3
7  20.84306 15.82117 23.9 2016-03-28T17:28:27.411Z        7   3
8  20.84308 15.82115 23.9 2016-03-28T17:28:28.411Z        8   3
9  20.84292 15.82108 23.9 2016-03-28T17:28:29.411Z        9   3
10 20.84307 15.82124 23.9 2016-03-28T17:28:30.412Z       10   3
以创建热图。我决定测量数据集中每个点到所有其他点的距离,并计算所有小于5米的距离,或任何数字。然后使用该信息对每个点进行颜色编码。e、 g.5m范围内有更多点的点将为红色,然后为橙色、黄色等

为了测量所有点之间的距离,我提出了以下函数:

numberClosePoints=function(df,i,radius){
  close_points=c()
  while(i<=nrow(df)){  # looping while i <= the number of rows in the data frame
    j = 1 # ... initializing j
    count = 0 # count of distances for each i
    while(j<=nrow(df)){# looping while j is < 5
      dist=pointDistance(c(geodf$lon[i],geodf$lat[i]), c(geodf$lon[j],geodf$lat[j]),lonlat=T)
      if (dist <= radius | is.nan(dist)){ # Add the is.nan because for some reason the distance between 5 and 5 was NaN and the function stops
        count = count + 1
      }
      j = j + 1   # incrementing j
    }
    close_points <- c(close_points,count-1)
    i = i + 1    # incrementing i
  }
  return(close_points)
}
numberClosePoints=函数(df,i,半径){
关闭_点=c()

虽然(这里有一个
dist()
函数,用于将点与所有其他点进行比较。也许你可以看看hexbins。另外,还有一个篮球版本,你可以作为灵感……或者你可以使用(你的数据,图形::smoothScatter(lon,lat))
。或者查看
帮助(“kde2d”,package=“MASS”)
。如果你给出一个可重复的例子,我打赌你会得到一些很好的解决方案。R中加速计算的第一条规则是避免循环!但我认为你的逻辑存在更大的问题。数据集中的每一行都是一个位置,如果行在时间上间隔相等,那么你所需要做的就是创建一个二维直方图。请参阅ggplot2的stat_density2d函数。为了简化计算,可以假设平面坐标,因为地球的曲线在运动场的尺度上是无关紧要的。