Delete fields in the attribute table

Share:

Description

Removes all fields except those listed in the fields parameter

Usage

1
ms_filter_fields(input, fields)

Arguments

input

spatial object to filter fields on. One of:

  • geo_json or character points, lines, or polygons;

  • geo_list points, lines, or polygons;

  • SpatialPolygonsDataFrame, SpatialLinesDataFrame, SpatialPointsDataFrame

fields

character vector of fields to retain.

Value

object with only specified attributes retained, in the same class as the input

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
library(geojsonio)
library(sp)

poly <- structure("{\"type\":\"FeatureCollection\",
                  \"features\":[{\"type\":\"Feature\",
                  \"properties\":{\"a\": 1, \"b\":2, \"c\": 3},
                  \"geometry\":{\"type\":\"Polygon\",
                  \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}]}",
                  class = c("json", "geo_json"))
poly <- geojson_sp(poly)
poly@data

# Filter (keep) fields a and b, drop c
out <- ms_filter_fields(poly, c("a", "b"))
out@data