diff --git a/DESCRIPTION b/DESCRIPTION index ae8689c..288bf48 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ Description: Classes and methods for spatial objects that have a registered time data (vector or raster data cubes) are handled by package 'stars'. Version: 0.2.0.9000 Depends: - sf (>= 1.0.7) + sf (>= 1.0.9) Imports: methods Suggests: knitr, diff --git a/NEWS.md b/NEWS.md index a02ebc0..d74e370 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,10 @@ # sftime (development version) +* Add a dedicated `tidyr::drop_na()` method for `sftime` objects. (See the same recent addition for `sf` objects [#1975](https://github.com/r-spatial/sf/pull/1975/)). + * Add a dedicated `dplyr::dplyr_reconstruct()` method for `sftime` objects. Relying on the method for `sf` objects caused erroneously column binding when the second object was a data frame without conflicting column names for the `sf` and time columns. In this case, a `sf` objects was returned, even though an `sftime` object should be returned. See also https://github.com/r-spatial/sf/issues/1958#issuecomment-1181982244. - * Add methods to convert `sftime` objects from: + Objects from the `spatstat` package classes (`ppp`, `psp`, `lpp`) + `sftrack` and `sftraj` objects from the `sftrack` package. diff --git a/R/init.R b/R/init.R index b300697..c31afb2 100644 --- a/R/init.R +++ b/R/init.R @@ -62,6 +62,7 @@ register_all_s3_methods <- function() { register_s3_method("tidyr", "separate_rows", "sftime") register_s3_method("tidyr", "unite", "sftime") register_s3_method("tidyr", "unnest", "sftime") + register_s3_method("tidyr", "drop_na", "sftime") } diff --git a/R/sftime.R b/R/sftime.R index db28005..a85b462 100644 --- a/R/sftime.R +++ b/R/sftime.R @@ -265,7 +265,8 @@ reclass_sftime <- function(x, time_column_name) { } #' @name st_sftime -#' @param value An object to insert into \code{x}. +#' @param value An object to insert into \code{x} or with which to rename +#' columns of \code{x}. #' @examples #' ## Assigning values to columns #' @@ -298,6 +299,18 @@ reclass_sftime <- function(x, time_column_name) { structure(NextMethod(), class = c("sftime", setdiff(class(x), "sftime"))) } + +##' name st_sftime +##' examples +##' # renaming column names +##' names(x)[1] <- "b" +##' +##' export +#"names<-.sftime" <- function(x, value) { +# out <- NextMethod() +# dplyr_reconstruct.sftime(out, x) +#} # ---todo: raises an error + #### printing #### #' Helper function to print time columns when printing an \code{sftime} object diff --git a/R/tidyverse.R b/R/tidyverse.R index f3de828..9b26e56 100644 --- a/R/tidyverse.R +++ b/R/tidyverse.R @@ -10,6 +10,7 @@ #' @inheritParams sf::tidyverse #' @inheritParams tidyr::pivot_longer #' @param x An object of class \code{sftime}. +#' @param y See \code{dplyr::`mutate-joins`}. #' @param .data An object of class \code{stime}. #' @return #' \itemize{ @@ -310,3 +311,22 @@ dplyr_reconstruct.sftime <- function(data, template) { } } + +#' @rdname tidyverse +#' @examples +#' ## drop_na +#' x1 %>% +#' mutate(z = c(1, 2, NA)) %>% +#' drop_na(z) +#' +#' x1 %>% +#' mutate(z = c(1, NA, NA)) %>% +#' drop_na(z) +#' +#' x1 %>% +#' mutate(time = replace(time, 1, NA)) %>% +#' drop_na(time) +drop_na.sftime <- function(data, ...) { + reclass_sftime(NextMethod(), time_column_name = attr(data, "time_column")) +} + diff --git a/man/st_sftime.Rd b/man/st_sftime.Rd index c347105..ea1c482 100644 --- a/man/st_sftime.Rd +++ b/man/st_sftime.Rd @@ -77,7 +77,8 @@ and return an \code{sf} object.} \item{op}{A function; geometrical binary predicate function to apply when \code{i} is a simple feature object.} -\item{value}{An object to insert into \code{x}.} +\item{value}{An object to insert into \code{x} or with which to rename +columns of \code{x}.} } \value{ \code{st_sftime}: An object of class \code{sftime}. diff --git a/man/tidyverse.Rd b/man/tidyverse.Rd index 4eef13f..9e2de66 100644 --- a/man/tidyverse.Rd +++ b/man/tidyverse.Rd @@ -31,47 +31,48 @@ \alias{separate.sftime} \alias{unite.sftime} \alias{separate_rows.sftime} +\alias{drop_na.sftime} \title{'tidyverse' methods for \code{sftime} objects} \usage{ -inner_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{inner_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -left_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{left_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -right_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{right_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -full_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{full_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -semi_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{semi_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -anti_join.sftime(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) +\method{anti_join}{sftime}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -filter.sftime(.data, ..., .dots) +\method{filter}{sftime}(.data, ..., .dots) -arrange.sftime(.data, ..., .dots) +\method{arrange}{sftime}(.data, ..., .dots) -group_by.sftime(.data, ..., add = FALSE) +\method{group_by}{sftime}(.data, ..., add = FALSE) -ungroup.sftime(.data, ...) +\method{ungroup}{sftime}(.data, ...) -rowwise.sftime(.data, ...) +\method{rowwise}{sftime}(.data, ...) -mutate.sftime(.data, ..., .dots) +\method{mutate}{sftime}(.data, ..., .dots) -transmute.sftime(.data, ..., .dots) +\method{transmute}{sftime}(.data, ..., .dots) -select.sftime(.data, ...) +\method{select}{sftime}(.data, ...) -rename.sftime(.data, ...) +\method{rename}{sftime}(.data, ...) -slice.sftime(.data, ..., .dots) +\method{slice}{sftime}(.data, ..., .dots) -summarise.sftime(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) +\method{summarise}{sftime}(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) -summarize.sftime(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) +\method{summarize}{sftime}(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) -distinct.sftime(.data, ..., .keep_all = FALSE) +\method{distinct}{sftime}(.data, ..., .keep_all = FALSE) -gather.sftime( +\method{gather}{sftime}( data, key, value, @@ -81,7 +82,7 @@ gather.sftime( factor_key = FALSE ) -pivot_longer.sftime( +\method{pivot_longer}{sftime}( data, cols, names_to = "name", @@ -98,25 +99,11 @@ pivot_longer.sftime( ... ) -spread.sftime( - data, - key, - value, - fill = NA, - convert = FALSE, - drop = TRUE, - sep = NULL -) +\method{spread}{sftime}(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL) -sample_n.sftime( - tbl, - size, - replace = FALSE, - weight = NULL, - .env = parent.frame() -) +\method{sample_n}{sftime}(tbl, size, replace = FALSE, weight = NULL, .env = parent.frame()) -sample_frac.sftime( +\method{sample_frac}{sftime}( tbl, size = 1, replace = FALSE, @@ -124,11 +111,11 @@ sample_frac.sftime( .env = parent.frame() ) -nest.sftime(.data, ...) +\method{nest}{sftime}(.data, ...) -unnest.sftime(data, ..., .preserve = NULL) +\method{unnest}{sftime}(data, ..., .preserve = NULL) -separate.sftime( +\method{separate}{sftime}( data, col, into, @@ -140,16 +127,16 @@ separate.sftime( ... ) -unite.sftime(data, col, ..., sep = "_", remove = TRUE) +\method{unite}{sftime}(data, col, ..., sep = "_", remove = TRUE) -separate_rows.sftime(data, ..., sep = "[^[:alnum:]]+", convert = FALSE) +\method{separate_rows}{sftime}(data, ..., sep = "[^[:alnum:]]+", convert = FALSE) + +\method{drop_na}{sftime}(data, ...) } \arguments{ \item{x}{An object of class \code{sftime}.} -\item{y}{A pair of data frames, data frame extensions (e.g. a tibble), or -lazy data frames (e.g. from dbplyr or dtplyr). See \emph{Methods}, below, for -more details.} +\item{y}{See \code{dplyr::`mutate-joins`}.} \item{by}{A character vector of variables to join by. @@ -425,4 +412,16 @@ x1 \%>\% mutate(z = c("1", "2,3,4", "5,6")) \%>\% separate_rows(z, convert = TRUE) +## drop_na +x1 \%>\% + mutate(z = c(1, 2, NA)) \%>\% + drop_na(z) + +x1 \%>\% + mutate(z = c(1, NA, NA)) \%>\% + drop_na(z) + +x1 \%>\% + mutate(time = replace(time, 1, NA)) \%>\% + drop_na(time) }