i'm currently working on the distribution of different species on an area, for 23 years.
I'll show you a sample of my data, it will be easier to get.
My dataframe, let's call it df.
x y
4001758 3138416
3990685 3088576
4002641 3078682
3946723 3153793
3975356 2974350
4001284 3137528
3946723 3153793
3946723 3153793
4000195 3103181
4000168 3103446
3969985 3104761
3905824 3107504
I created a raster
from .tif file.
Here is what i want to do :
I want to extract the value for each cell (with the extract() function) from my raster
, where the coordinates
corresponding to this value, are also in df.
I did it, so i'm still working on it, and for now, i didn't see any errors.
But the difficulty that i have is that i want to also get the coordinates
corresponding for each value in my result of the extract
.
Here is what i did :
test_extractcropland<-extract(cropland,df_coord)
(cropland is my raster, df_coord is a dataframe, you have a sample, above)
Do you have any idea of how i could do that ?
I looked on this forum, and stackoverflow (and others), but i didn't find something that i could adapt for my problem, or it's more likely that i didn't see how i could adapt it.
Here is some data : https://we.tl/3xe509KSZO
"coord_for_extract.csv" is what i want to filter with, the other are just raster if you want to test.
df_coord is coord_for_extract, r is my raster
Best Answer
Convert
df
to asp
object and usesp = TRUE
as argument.What you have:
What you need:
You'll be able to obtain coordinates from this
sp
object.A workaround could be:
Get coordinates of raster cells
First, use
cellnumbers = T
withextract()
:coordinates()
retrieves coordinates by cell number, so useresult
to subset:And, with original table: