简体   繁体   English

如何将嵌套的 for 循环操作转换为 R 中更高效的代码

[英]How to transform a nested for-loop operation to a more efficient code in R

I am a dilettante when it comes to R coding.在 R 编码方面,我是个业余爱好者。 I am trying to run the following code for one of the tasks.我正在尝试为其中一项任务运行以下代码。 My basic purpose is to count the number of attractions within the proximity of 2kms of a specific location, both attractions, and the locations are specified by respective longitude and latitude.我的基本目的是计算特定位置2kms附近的景点数量,两个景点,位置由各自的经度和纬度指定。 The number of records in the main data set is around 29K and while the number of attractions is 28. How can I convert the following code in a better performing R code instead (the current one is really crude and not at all a good practice)主数据集中的记录数约为 29K,而景点数为 28。如何将以下代码转换为性能更好的 R 代码(当前的代码非常粗糙,根本不是一个好习惯)

for(i in 1:nrow(mainData)) {
  attr_count[i] = 0  
  loc_coord = c(mainData$longitude[i],mainData$latitude[i])
  for(j in 1:nrow(ny_attractions)) {
    attr_coord = c(ny_attractions$lon[j],ny_attractions$lat[j])
    dist = distVincentySphere(attr_coord,loc_coord)
    if(dist <= 2000) {
      attr_count[i] = attr_count[i] + 1
    } 
  }
}

[EDIT]: My apologies for not putting it clearly earlier. [编辑]:我很抱歉没有早点说清楚。 Here's an example of what I am trying to achieve.这是我试图实现的一个例子。 I have 2 data sets -我有 2 个数据集 -

Dataset - 1 (NYC_attractions) (27 records)数据集 - 1 (NYC_attractions)(27 条记录)

在此处输入图片说明

Dataset-2 (master data for house listings) (29K records)数据集 2(房屋清单的主数据)(29K 记录)

在此处输入图片说明

Now, I need to add one more column (num_of_attractions) in Dataset-2, representing the number of attractions within 2Kms of the specified listing (ie per record in data set-2)现在,我需要在 Dataset-2 中再添加一列 (num_of_attractions),表示指定列表 2Kms 内的景点数量(即数据集 2 中的每条记录)

Hope, this explains the problem希望,这可以解释问题

Thanks谢谢

Hello your question is partly answered here https://stackoverflow.com/a/49860968/3042154 .您好,您的问题在这里得到了部分解答https://stackoverflow.com/a/49860968/3042154 As you use geodetic coordinates (lat/lon) instead of projected coordinates (meters) it can be done in to steps.当您使用大地坐标(纬度/经度)而不是投影坐标(米)时,可以分步完成。 First roughly select potential neighbours using euclidian distance using given answer then refine the selection by using your distance首先使用给定的答案使用欧几里德距离粗略选择潜在邻居,然后使用您的距离细化选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM