4

From this code:

na_cells <- as.data.frame(extract(r_temp, poly_contour, cellnumbers = TRUE))

Why do I get this warning?

Warning in `[<-`(`*tmp*`, cnt, value = p@polygons[[i]]@Polygons[[j]]) :
  implicit list embedding of S4 objects is deprecated

Here are some details on my raster and shapefile:

> r_temp
class       : RasterLayer 
dimensions  : 4876, 9864, 48096864  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : 188384.5, 484304.5, 4914481, 5060761  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : C:\Users\Pierre\AppData\Local\Temp\RtmpKWmDgF\raster\r_tmp_2016-05-16_204325_10956_97931.grd 
names       : layer 
values      : 1, 8  (min, max)

> poly_contour
class       : SpatialPolygons 
features    : 1 
extent      : 189644.5, 483014.5, 4914961, 5060761  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 

Recently, I have installed a new version of R (version 3.3.0). Before that, my code has worked. Is it possible that this warning is caused my version of R ?

5
  • 1
    It is very possible. Make sure to update all the installed packages as well using installr package. This may solve the problem. Commented May 17, 2016 at 6:15
  • The output of extract is a recursive list of cell numbers and values, it needs tidying to a data.frame
    – mdsumner
    Commented May 17, 2016 at 8:41
  • 2
    Sorry, this comment and my answer is a diversion - it's a change in R 3.3.0 - you'll need to wait for the raster package to be updated for the warning to go away.
    – mdsumner
    Commented May 17, 2016 at 14:29
  • 1
    Raster has been updated recently, this warning no longer populates.
    – Badger
    Commented Jun 7, 2016 at 14:41
  • @Badger. I just had it after updating Commented Feb 27, 2020 at 19:12

1 Answer 1

4

The output of extract is a list of cell numbers and values stored in matrices, it needs tidying to a data frame. (Data frames are best since they can store different types of data, like integer cell numbers and numeric values - which is essentially what cellnumbers=TRUE is for).

library(raster)
r <- raster(volcano)
## simplify the values
r <- (r %/% 20) * 20

p <- rasterToPolygons(r, dissolve = TRUE)

cells <- extract(r, p, cellnumbers = TRUE)
## note there's a list element for every individual polygon object
class(cells)
#[1] "list"
length(cells)
#[1] 6
nrow(p)
#[1] 6

## each element is a matrix, so standard tidying is easy
library(dplyr)
## (see purrr for more recommended ways to map functions like this)
d <- bind_rows(lapply(cells, as.data.frame), .id = "polygon")

head(d)
#  polygon cell value
#1       1    1   100
#2       1    2   100
#3       1    3   100
#4       1    4   100

Then you can use the dplyr tools in the usual ways, piping or chaining as you like:

summarize(group_by(d, polygon), mnval = mean(value))

Source: local data frame [6 x 2]

  polygon      mnval
    <chr>       <dbl>
1       1         100
2       2         120
3       3         140
4       4         160
5       5         180
6       6          80

The reason you'd work on these cell numbers individually is to make future extractions faster, or converting back to cell geometry etc., if you had changing layers coming in in a loop. (Then you don't redo the math to figure out which cells belong to which polygons).

Something like

d$value <- extract(r2, d$cell)

and so on for whatever summary etc. needed.

You don't have to use dplyr for this kind of work, but I highly recommend it. If you just want raster::extract to do your summary, give it a function rather than ask for cell numbers i.e.

cells <- extract(r, p, fun = mean)

(If the first argument to extract is multi-layered it's already optimized in the cellnumbers way, but that doesn't always scale so more flexibility is good).

3
  • Where exactly do you do "the tidying" as you describe it? The way I see it you simply call extract with a raster an a Polygon p?! Or is it the rasterToPolygon function? What is it doing exactly? That inside-r.org/packages/cran/raster/docs/rasterToPolygons does not really help...
    – four-eyes
    Commented Jun 1, 2016 at 15:38
  • 'd <- bind_rows(lapply(...'. "cells" here is a list of matrices, and bind_rows turns it into a single table of polygon id, cell id, and pixel value. (Raster doesn't recommend working this way, so it's not documented specifically )
    – mdsumner
    Commented Jun 1, 2016 at 16:03
  • okay, but what I do not understand ist how this line p <- rasterToPolygons(r, dissolve = TRUE) prevents the implicit list embedding... error. That one occurs because he using extract() and not bind_rows....?!
    – four-eyes
    Commented Jun 1, 2016 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.