-
-
Save dholstius/58818dc9bbb88968ec0b to your computer and use it in GitHub Desktop.
#' Filter (subset) a Spatial*DataFrame object | |
#' | |
#' @param object a \code{Spatial*DataFrame} | |
#' @param ... see \link{subset} | |
#' @param .dots | |
#' | |
#' @importFrom lazyeval all_dots lazy_eval | |
#' | |
#' @return a subset of the original \code{Spatial*DataFrame} | |
#' | |
#' @seealso subset | |
#' | |
#' @examples | |
#' library(dplyr) | |
#' library(sp) | |
#' data(meuse, package = "sp") | |
#' j <- which(names(meuse) %in% c("x", "y")) | |
#' meuse_spdf <- SpatialPointsDataFrame(meuse[, j], meuse[, -j]) | |
#' high_copper <- filter(meuse_spdf, copper > median(copper)) | |
#' stopifnot(inherits(high_copper, class(meuse_spdf))) | |
#' | |
#' @export | |
filter_.SpatialDataFrame <- function (object, ..., .dots) { | |
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) | |
masks <- lazyeval::lazy_eval(dots, data = as.data.frame(object@data)) | |
subset(object, Reduce(`&`, masks)) | |
} | |
# FIXME: @describeIn makes roxygen complain "Don't know how to describe s3method in s3method" | |
# @describeIn filter_.SpatialDataFrame | |
filter_.SpatialLinesDataFrame <- filter_.SpatialDataFrame | |
# @describeIn filter_.SpatialDataFrame | |
filter_.SpatialPointsDataFrame <- filter_.SpatialDataFrame | |
# @describeIn filter_.SpatialDataFrame | |
filter_.SpatialPolygonsDataFrame <- filter_.SpatialDataFrame |
I don't think your example here applies; as meuse is already a data frame
Hi @dgrtwo! Almost --- meuse
is a data.frame
, but the statement:
coordinates(meuse) <- ~x+y
... promotes meuse
to a SpatialPointsDataFrame
. (Same syntax found in help(meuse)
).
dplyr::filter
doesn't work out-of-the-box with Spatial*DataFrame
objects. What will work is subset
, but it needs a little help in the absence of non-standard evaluation.
I'll edit the example to make this a little more clear. Thanks for the comment!
Edited. Also fixed &&
where &
should have been. World of difference.
I find this function super useful and wish dplyr would support Spatial*DataFrame directly! Would be great to have the same for all the dplyr verbs. Particularly mutate
, transmute
, select
, slice
, rename
. Others don't really make sense or are more complicated. group_by
, for example, would be nice in combination with mutate
but summarise
would require joining polygons, which could be useful but more complicated.
Edit: All the joins as well (left_join
etc)
I've done mutate, transmute, select, slice, rename, arrange and distinct. Shall we create a simple package for just these methods? Here's what I have: https://gist.github.com/mdsumner/85d76f8b33e26a427c3d Originally I was seeing this through a different lens of flattening to a table - but that might be a good way to handle joins.
I think summarise is best left undefined for now.
There's probably a bunch of things I'm not doing properly here, so happy for any feedback / discussion.
And here's my packaging of it: https://github.com/mdsumner/spbabel
Suspected issues
Please suggest fixes in the form of comments! :-)
This code should maybe:
NextMethod
and/orUseMethod
@method
in the roxygen skeletonLet's figure out whose repository it should live in, and then fix it up! :-)