Created
March 16, 2016 14:42
-
-
Save mdsumner/d4bf42ac5b7544f60fdf to your computer and use it in GitHub Desktop.
S3 mutate_ for sp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Mutate for Spatial | |
#' | |
#' mutate . . . | |
#' | |
#' @export | |
#' @examples | |
#' library(sp) | |
#' library(maptools) | |
#' data(wrld_simpl) | |
#' library(dplyr) | |
#' library(spbabel) ## devtools::install_github("mdsumner/spbabel", ref = "pipe") | |
#' w2 <- wrld_simpl %>% mutate(NAME = "allthesame") | |
#' | |
#' ## how to pipe a reprojection? some special formula syntax for ~x+y ?? ~lon+lat ?? | |
mutate_.Spatial_DataFrame <- function(.data, ..., .dots) { | |
dots <- lazyeval::all_dots(.dots, ..., all_named = TRUE) | |
pr4 <- proj4string(.data) | |
dgeom <- sptable(.data) | |
dat <- as.data.frame(.data) | |
spFromTable(mutate_(dgeom, .dots = dots), crs = pr4, attr = dat) | |
} | |
#' @export | |
mutate_.SpatialPolygonsDataFrame <- mutate_.Spatial_DataFrame | |
#' @export | |
mutate_.SpatialLinesDataFrame <- mutate_.Spatial_DataFrame | |
#' @export | |
mutate_.SpatialPointsDataFrame <- mutate_.Spatial_DataFrame | |
#' @export | |
mutate_.SpatialMultiPointsDataFrame <- mutate_.Spatial_DataFrame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you can make this simpler in two ways:
Dispatch on the
Spatial
class instead of each subclass individuallyWrite a
sptable<-
function so you can do