[GIS] Applying filter with the sf package not working

rsf

Question:
I have a sf-object in R and want to select a few polygons according to their location name. I would like to apply the dpylr approach using the filter function (analog to this example). However, this does not work and returns an error message.

Is this a bug, is there a problem with the data, or is something wrong with the code?

Example:

> wards %>%
+  filter(NAME_WARD == "Bumbuta")
Error in data.matrix(data) : 
  (list) object cannot be coerced to type 'double'
In addition: Warning messages:
1: In data.matrix(data) : NAs introduced by coercion
2: In data.matrix(data) : NAs introduced by coercion
3: In data.matrix(data) : NAs introduced by coercion

Data:

> wards
Simple feature collection with 5 features and 6 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 35.82647 ymin: -4.968201 xmax: 36.3376 ymax: -4.55612
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs 
   NAME_REG NAME_DIST NAME_WARD ID_REG ID_DIST ID_WARD                       geometry
1    Dodoma    Kondoa   Bumbuta     01    0101 0101011 MULTIPOLYGON (((35.93715495...
2    Dodoma    Kondoa      Pahi     01    0101 0101021 MULTIPOLYGON (((35.93456275...
3    Dodoma    Kondoa     Haubi     01    0101 0101041 MULTIPOLYGON (((35.92014452...
4    Dodoma    Kondoa   Kalamba     01    0101 0101051 MULTIPOLYGON (((35.88398049...
5    Dodoma    Kondoa   Kwadelo     01    0101 0101061 MULTIPOLYGON (((36.33663165...

> str(wards)
Classes ‘sf’ and 'data.frame':  5 obs. of  7 variables:
 $ NAME_REG : chr  "Dodoma" "Dodoma" "Dodoma" "Dodoma" ...
 $ NAME_DIST: chr  "Kondoa" "Kondoa" "Kondoa" "Kondoa" ...
 $ NAME_WARD: chr  "Bumbuta" "Pahi" "Haubi" "Kalamba" ...
 $ ID_REG   : chr  "01" "01" "01" "01" ...
 $ ID_DIST  : chr  "0101" "0101" "0101" "0101" ...
 $ ID_WARD  : chr  "0101011" "0101021" "0101041" "0101051" ...
 $ geometry :sfc_MULTIPOLYGON of length 5; first list element: List of 1
  ..$ :List of 1
  .. ..$ : num [1:200, 1:2] 35.9 35.9 35.9 35.9 35.9 ...
  ..- attr(*, "class")= chr  "XY" "MULTIPOLYGON" "sfg"
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA
  ..- attr(*, "names")= chr  "NAME_REG" "NAME_DIST" "NAME_WARD" "ID_REG" ...

Best Answer

You've not attached the dplyr package:

> e %>% filter(ADMIN_NAME=="Durham")
Error in data.matrix(data) : 
  (list) object cannot be coerced to type 'double'

When you do, you'll see that dplyr stomps on the base R filter function with its own filter function:

> library(dplyr)

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    **filter**, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

And then it works:

> e %>% filter(ADMIN_NAME=="Durham")
Simple feature collection with 1 feature and 7 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -6.359723 ymin: 49.89083 xmax: 1.771111 ymax: 55.80611
epsg (SRID):    NA
proj4string:    NA
  ADMIN_NAME  AREA PERIMETER ADMIN2_ ADMIN2_ID  GMI_ADMIN2 COUNTRY
1     Durham 0.335     3.147      18        18 GBR-ENG-DRH England
                        geometry
1 MULTIPOLYGON(((-2.305007934...

A better way is to be explicit about where filter is coming from:

> e %>% dplyr::filter(ADMIN_NAME=="Durham")
Simple feature collection with 1 feature and 7 fields
geometry type:  MULTIPOLYGON

then any code you have that uses the base R filter won't mysteriously break because dplyr is attached.