You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let me just point out a mistake I did in the begining, which might occur only in the case of points data. when you want to combine a column from the original spatial query with the output of cellnumbers() . Here is a reproducible example
library(sf)
library(tabularaster)
# create some labeled data
sq <- seq(0, 1, by = 0.1)
p <- st_as_sf(expand.grid(sq, sq), coords = c("Var1", "Var2"))
p$label <- rep(c("A", "B"), length.out = nrow(p))
# load raster data
r <- raster(volcano)
# extract volcano data and append labels (e.g. for descriptive stats or regression)
cn <- cellnumbers(r, p) %>% mutate(elev = extract(r, cell_),
label = p$label) # and here is the mistake. It is very tempting to
# to simply `cbind`
Simply cbinding the output of cellnumbers() back to the original spatial object (or transfering any extra columns from p to cn as I am showing here) is wrong because the order of p is not preserved in cn. The correct way would be to join by row_id
cn <- cellnumbers(r, p) %>% mutate(elev = extract(r, cell_))
p <- rowid_to_column(p) # additional step needed to create a join column
p <- left_join(p, cn, by = c('rowid' = 'object_'))
So i just wanted to point that. Or is there better way to perform this type of extraction followed by combination with original point data?
I see several ways to improve this
order the output of cellnumbers() by object_.
Maybe consider renaming object_ to rowid as semanticaly speaking they are the same.
Or provide the option in cellnumbers call to select a column from the spatial object to become the object_ column.
The text was updated successfully, but these errors were encountered:
Hi,
I find
cellnumbers()
very very usefull!Let me just point out a mistake I did in the begining, which might occur only in the case of points data. when you want to combine a column from the original spatial query with the output of
cellnumbers()
. Here is a reproducible exampleSimply
cbind
ing the output ofcellnumbers()
back to the original spatial object (or transfering any extra columns fromp
tocn
as I am showing here) is wrong because the order ofp
is not preserved incn
. The correct way would be to join byrow_id
So i just wanted to point that. Or is there better way to perform this type of extraction followed by combination with original point data?
I see several ways to improve this
cellnumbers()
byobject_
.object_
torowid
as semanticaly speaking they are the same.cellnumbers
call to select a column from the spatial object to become theobject_
column.The text was updated successfully, but these errors were encountered: