I am using the following code to extract raster values over a SpatialPolygonDataFrame.
result<-extract(Stack, polygons)
>Stack
class : RasterStack
dimensions : 634, 862, 546508, 13 (nrow, ncol, ncell, nlayers)
resolution : 10, 10 (x, y)
extent : 492660, 501280, 4378960, 4385300 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
names : L2A_T29SM//21_B02_10m//etc.
min values : 1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000, 1.0000000, 119.0000000, 88.0000000, 1.0000000, -0.9932203, -0.9959184, 1.0000000
max values : 1.460000e+04, 1.547100e+04, 1.485900e+04, 1.547600e+04, 1.168700e+04, 1.131400e+04, 1.109100e+04, 1.084500e+04, 1.451000e+04, 1.105000e+04, 9.996238e-01, 9.992918e-01, 4.000000e+00
> polygons
class : SpatialPolygonsDataFrame
features : 13
extent : 493428.6, 499603.9, 4379481, 4383781 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=29 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 3
names : id, ID_1, LC
min values : 1, 1, Agri
max values : 9, 4, Water
Then I want to create a dataframe out of it but I received the following error:
resultdf<-as.data.frame(result)
Error: arguments imply differing number of rows: 225, 320, 286, 351, 68, 79, 41, 63, 70, 171, 72, 158, 497.
From what I check in the forum it is a problem with the data structure since the dataframe would not have the same length; how can I solve it?
dfwith one column per layer in thestack+ one extra column for polygonID_1repeated for each pixel under the polygon with thatid_1(notID- according to my polygon attributes). Then one row per pixel inside the polygons. The solution proposed by @RoberH almost work although I adapated it.result<-extract(stack, polygons, df=TRUE)The problem is that it's usingpolygon$IDinstead ofpolygon&ID_1– GCGM Dec 19 '17 at 08:43