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. 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. 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)
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 -
Dataset - 1 (NYC_attractions) (27 records)
enter image description here
Dataset-2 (master data for house listings) (29K records)
enter image description here
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 (i.e. per record in data set-2)
Hope, this explains the problem
Thanks
r for-loop
add a comment |
I am a dilettante when it comes to R coding. 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. 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)
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 -
Dataset - 1 (NYC_attractions) (27 records)
enter image description here
Dataset-2 (master data for house listings) (29K records)
enter image description here
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 (i.e. per record in data set-2)
Hope, this explains the problem
Thanks
r for-loop
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packagessp
,rgeos
orsf
. Please give us a reproducible example.
– JRR
Nov 25 '18 at 8:25
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28
add a comment |
I am a dilettante when it comes to R coding. 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. 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)
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 -
Dataset - 1 (NYC_attractions) (27 records)
enter image description here
Dataset-2 (master data for house listings) (29K records)
enter image description here
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 (i.e. per record in data set-2)
Hope, this explains the problem
Thanks
r for-loop
I am a dilettante when it comes to R coding. 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. 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)
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 -
Dataset - 1 (NYC_attractions) (27 records)
enter image description here
Dataset-2 (master data for house listings) (29K records)
enter image description here
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 (i.e. per record in data set-2)
Hope, this explains the problem
Thanks
r for-loop
r for-loop
edited Nov 26 '18 at 1:41
Puneet Matai
asked Nov 25 '18 at 7:58
Puneet MataiPuneet Matai
12
12
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packagessp
,rgeos
orsf
. Please give us a reproducible example.
– JRR
Nov 25 '18 at 8:25
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28
add a comment |
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packagessp
,rgeos
orsf
. Please give us a reproducible example.
– JRR
Nov 25 '18 at 8:25
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packages
sp
, rgeos
or sf
. Please give us a reproducible example.– JRR
Nov 25 '18 at 8:25
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packages
sp
, rgeos
or sf
. Please give us a reproducible example.– JRR
Nov 25 '18 at 8:25
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28
add a comment |
1 Answer
1
active
oldest
votes
Hello your question is partly answered here 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
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465664%2fhow-to-transform-a-nested-for-loop-operation-to-a-more-efficient-code-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hello your question is partly answered here 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
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
add a comment |
Hello your question is partly answered here 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
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
add a comment |
Hello your question is partly answered here 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
Hello your question is partly answered here 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
answered Nov 25 '18 at 20:19
Billy34Billy34
22616
22616
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
add a comment |
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Awesome!! Not partially, this answers my question completely. However, I need to still figure out whether it works with longitude and latitude instead of points. Thank you so much for your help. Really appreciate it.
– Puneet Matai
Nov 26 '18 at 1:31
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
Nice to hear it! If you consider your question answered please mark it so or upvote my answer :-)
– Billy34
Nov 26 '18 at 19:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465664%2fhow-to-transform-a-nested-for-loop-operation-to-a-more-efficient-code-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It looks like you are trying to manipulate spatial data. There are a lot of resources to do what you want to do in packages
sp
,rgeos
orsf
. Please give us a reproducible example.– JRR
Nov 25 '18 at 8:25
Please post sample data to work with. Also post what the expected outcome should look like
– Wally Ali
Nov 25 '18 at 8:28