Присвоить данные ближайшей точке отбора в R - PullRequest
0 голосов
/ 24 января 2020

Я очень плохо знаком с R и хотел бы получить помощь по назначению точки данных (набор данных 1) ближайшей точке выборки (набор данных 2). Точки отбора проб равномерно распределены вдоль линии с интервалом 100 м, а точки данных были взяты вдоль этой линии. Моя цель - создать CSV-файл с ближайшей точкой выборки, назначенной каждой точке данных. Мои данные и точки выборки являются фреймами данных.

Вот мой код:

sampling.points <- read.csv("pts.csv",header=T)

data <- read.csv("data.csv",header=T)

library("sf")

sp_sf <- st_as_sf(sampling.points, coords = c("x", "y"), crs = 24500, agr="NA_agr_")

data_sf = st_as_sf(data, coords = c("x", "y"), crs = 24500, agr = "NA_agr_")

library("nngeo")

st_nn(x, y, sparse = TRUE, k = 1, maxdist = 100,
      returnDist = FALSE, progress = TRUE)

Мне удалось сгенерировать все до последнего предложения - Код ошибки, который я не могу вернуть чтобы понять:

Ошибка в UseMethod ("st_geometry"): нет применимого метода для st_geometry, примененного к объекту класса "c ('matrix', 'double', 'Numberri c ') ".

Спасибо, если кто-то может мне помочь с этим.

sampling.points <- structure(list(point = structure(1:10, .Label = c("TC01", "TC02",  "TC20", "TC21", "TC22",
"TC23", "TC24", "TC25", "TC26", "TC27",  "TC28", "TC29", "TC30",
"TC31", "TC32", "TD01", "TD02", "TD06",  "TD07", "TD08", "TD09",
"TD10", "TD11", "TD12", "TD13", "TQ25",  "TQ26", "TQ27", "TQ28",
"TQD02", "TQD03", "TQD04"), class = "factor"), 
 y = c(1.34285, 1.34314, 1.34373, 1.34416, 1.34457, 1.34502, 
 1.34543, 1.34586, 1.34575, 1.34593), x = c(103.7791, 103.7789, 
 103.7787, 103.7787, 103.7788, 103.7789, 103.7791, 103.7792, 
 103.7787, 103.7784)), row.names = c(NA, 10L), class = "data.frame")

data <- structure(list(point = structure(c(2L, 19L, 6L, 7L, 16L, 16L,  18L, 4L, 7L, 10L), .Label = c("CH651", "CH652",
 "CH653", "CH654",  "CH655", "CH656", "CH657", "CH658", "CH659",
 "CH660", "CH661",  "CH662", "CH663", "CH664", "CH665", "CH666",
 "CH667", "CH668",  "CH669", "CH670", "CH671", "CH672", "CH673",
 "CH674", "CH675",  "CH676", "CH677", "CH678", "CH679", "CH680",
 "CH681", "CH682",  "CH683", "FQ419", "FQ420", "FQ421", "FQ422",
 "FQ423", "FQ424",  "FQ425", "FQ426", "FQ427", "FQ428", "FQ429",
 "FQ430", "FQ431",  "FQ432", "FQ433", "FQ434", "FQ435", "FQ436",
 "FQ437", "JT4490",  "JT4491", "JT4492", "JT4493", "JT4494", "JT4495",
 "JT4496", "JT4497",  "JT4498", "JT4499", "JT4500", "JT4501", "JT4502",
 "JT4503", "JT4504",  "JT4505", "JT4506", "JT4507", "JT4508", "JT4509",
 "JT4510", "JT4511",  "JT4512", "JT4513", "JT4514", "JT4515"), class =
 "factor"), x = c(1.345922006, 
 1.34295498, 1.346109007, 1.345989984, 1.344331037, 1.344331037, 
 1.343491003, 1.346088974, 1.345989984, 1.345756967), y = c(103.776551, 
 103.778987, 103.777546, 103.777906, 103.778766, 103.778766, 103.778702, 
 103.777206, 103.777906, 103.779213), taxon = structure(c(3L,  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("#N/A", "Amphibian",  "Bird",
 "Butterfly", "Mammal", "Odonate", "Reptile"), class = "factor"), 
     scientific_name = structure(c(2L, 2L, 3L, 4L, 4L, 5L, 5L, 
     6L, 6L, 6L), .Label = c("Accipiter sp.", "Acridotheres javanicus", 
     "Aegithina tiphia", "Aerodramus sp.", "Aethopyga siparaja", 
     "Aplonis panayensis", "Bronchocela cristatella", "Callosciurus notatus", 
     "Caprimulgus macrurus", "Caprimulgus sp.", "Chalcorana labialis", 
     "Chrysophlegma miniaceum", "Cinnyris jugularis", "Cratilla metallica", 
     "Cupha erymanthis lotis", "Curetis santana malayica", "Cynopterus brachyotis", 
     "Dicaeum cruentatum", "Dicaeum sp.", "Dicaeum trigonostigma", 
     "Dicrurus paradiseus", "Dinopium javanense", "Duttaphrynus melanostictus", 
     "Elymnias hypermnestra agina", "Eooxylides tharis distanti", 
     "Eudynamys scolopaceus", "Eurema sari sodalis", "Eurema sp.", 
     "Flying squirrel sp.", "Galeopterus variegatus", "Gallus gallus", 
     "Gracula religiosa", "Graphium sp.", "Ideopsis vulgaris macrina", 
     "Insect bat", "Insect Bat", "Jamides bochus nabonassar", 
     "Lexias pardalis dirteana", "Limnonectes plicatellus", "Loriculus galgulus", 
     "Macaca fascicularis", "Macronus gularis", "Microhyla butleri", 
     "Microhyla heymonsi", "Muscicapa dauurica", "Muscicapa sibirica", 
     "Mycalesis visala phamis", "Nacaduba sp.", "Nyctixalus pictus", 
     "Oriolus chinensis", "Orthetrum glaucum", "Orthotomus atrogularis", 
     "Orthotomus sericeus", "Orthotomus sutorius", "Otus lempiji", 
     "Paradoxurus musangus", "Phylloscopus borealis", "Polypedates leucomystax", 
     "Polyura hebe plautus", "Psilopogon lineatus", "Psittacula longicauda", 
     "Pulchrana laterimaculata", "Pycnonotus plumosus", "Rallina fasciata", 
     "Rattus sp.", "Rattus sp. ", "Sundasciurus tenuis", "Sus scrofa", 
     "Tanaecia pelea pelea", "Treron vernans", "Tyriobapta torrida", 
     "Zosterops simplex"), class = "factor"), local_status = structure(c(7L, 
     7L, 7L, 1L, 1L, 7L, 7L, 7L, 7L, 7L), .Label = c("#N/A", "Critically Endangered", 
     "Endangered", "Least Concern", "Not Assessed", "Not listed", 
     "Not Listed", "Vulnerable"), class = "factor"), indivs = c(1L, 
     6L, 1L, 2L, 1L, 1L, 1L, 2L, 10L, 100L), seen_heard = structure(c(2L, 
     2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L), .Label = c("Heard", 
     "Seen"), class = "factor"), method = structure(c(2L, 2L, 
     2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Incidental", 
     "Transect"), class = "factor")), row.names = c(NA, 10L), class = "data.frame")
...