"NN_Function" to measure the average distance from each fishnet grid cell centroid to its k nearest neighbors.

Im working on a topic for risk prediction using this Markdown tutorial.

Under the section of [Wrangle Land Use parcels]

(GitHub - urbanSpatial/PredictingFireRisk: A #rspatial workshop on predicting fire risk in San Francisco)

he used the nn_function, and you can see that the outcome was good.
Here is my same code:

st_c <- st_coordinates
st_coid <- st_centroid

Build_net <-
Build_net %>%
mutate(
Build.nn =
nn_function(st_c(st_coid(Build_net)), st_c(HighRiseB),3))

head(Build_net)

When I run the code, this error returns me:

Error in stopifnot(): :information_source: In argument: Build.nn = nn_function(st_c(st_coid(Build_net)), st_c(HighRiseB), 3). Caused by error in **nn_function()`: ! could not find function "nn_function" Backtrace: 1. Build_net %>% ... 7. dplyr:::mutate.data.frame(...) 8. dplyr:::mutate_cols(.data, dplyr_quosures(...), by) 10. dplyr:::mutate_col(dots[[i]], data, mask, new_columns) 11. mask$eval_all_mutate(quo) 12. dplyr (local) eval()

[image] Show Traceback

Error in stopifnot(!inherits(x, "sf"), !missing(sf_column_name), !missing(agr)) : Caused by error in nn_function(): ! could not find function "nn_function"

I want to measure the average distance from each fishnet grid cell centroid (my destination layer:Distance from) to its k nearest vacant neighbors (my another point feature layer as target: Distance to)

Best,
Alireza

The code block below does this using the nn_function - a custom function created for this purpose

suggests that this is the author's own function, not included in any of the libraries used. My suggestion is to open an issue on the github site or otherwise correspond with the author.

1 Like

Hi,
Dear "technocrat"

Thank you for kind answer. :pray:
After my first comment I read the text carefully for several times. Just like you, I realized that something is wrong here.

:thinking:
You know, the author of that tutorial passed away in 2021 unfortunately. So, I searched same projects. Then I found the same code and updated it as my project aim. Now it is working for me. This is the code where the tutorial was not:
nn_function <- function(measureFrom, measureTo, k) {
nn <- get.knnx(measureTo, measureFrom, k)$nn.dist
output <-
as.data.frame(nn) %>%
rownames_to_column(var = "thisPoint") %>%
gather(points, point_distance, V1:ncol(.)) %>%
arrange(as.numeric(thisPoint)) %>%
group_by(thisPoint) %>%
summarize(pointDistance = mean(point_distance)) %>%
arrange(as.numeric(thisPoint)) %>%
dplyr::select(-thisPoint) %>%
as.data.frame()
names(output) <- c("dist.nn")
return(output)
}

dataXY_function <- function(data) {
dataXY <- data %>%
st_coordinates() %>%
as.data.frame() %>%
dplyr :: select(X,Y) %>%
as.matrix()
return(dataXY)

Best regards
Alireza

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.