-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tidyverse function: drop_na #1578
Comments
Hi @gregleleu , Could you give reprex? I did this, the geometry column doesn't seem to drop. df <- sf::st_as_sf(
tibble::tibble(x = c(1, 2, NA),
y = c("a", NA, "b"),
lng = c(20.1, 20.2, 20.1),
lat = c(-1.1, -1.2, -1.2) ),
coords = c("lng", "lat"))
tidyr::drop_na(df)
#> # A tibble: 1 x 3
#> x y geometry
#> <dbl> <chr> <POINT>
#> 1 1 a (20.1 -1.1) Created on 2021-01-15 by the reprex package (v0.3.0) |
It looks like the class is dropped. |
Aaah, ok thanks. I will look into it. |
If this looks good for library(roxygen2)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(tibble)
library(tidyr)
#' @name tidyverse
#' @param … see original function docs
#' @examples
#' library(tidyr)
#' df <- st_as_sf(
#' tibble::tibble(x = c(1, 2, NA,3),
#' y = c(“a”, NA, “b”, “a”),
#' lng = c(20.1, 20.2, 20.1, 20.4),
#' lat = c(-1.1, -1.2, -1.2, -1.4)),
#' coords = c(“lng”, “lat”))
#' drop_na(df, x)
#' drop_na(df)
t_n <- function(data, ...){
if (!requireNamespace("tidyr", quietly = TRUE))
stop("tidyr required: install first?")
class(df) <- setdiff(class(df), "sf")
sf::st_as_sf(tidyr::drop_na(df, ...))
}
df <- sf::st_as_sf(
tibble::tibble(x = c(1, 2, NA,3),
y = c("a", NA, "b", "a"),
lng = c(20.1, 20.2, 20.1, 20.4),
lat = c(-1.1, -1.2, -1.2, -1.4)),
coords = c("lng", "lat"))
t_n(df)
#> Simple feature collection with 2 features and 2 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 20.1 ymin: -1.4 xmax: 20.4 ymax: -1.1
#> CRS: NA
#> # A tibble: 2 x 3
#> x y geometry
#> <dbl> <chr> <POINT>
#> 1 1 a (20.1 -1.1)
#> 2 3 a (20.4 -1.4) Created on 2021-01-18 by the reprex package (v0.3.0) Thanks, |
No doubt this will mostly work, but I'd appreciate if you'd follow the pattern e.g. given by |
Sorry about that, I am new to open source contribution and learning. Yes, happy to include for corner cases - following arrange.sf pattern. |
Any progress? Just noticed that drop_na mysteriously caused some things to fail. Took me a while to see it was changing the class of my custom tibble.... |
I just tried making a PR (it's a pretty simple change) but I can't get devtools::check to work (it's not finding sqlite3 apparently, and I could find how to set --with_sqlite3_lib) but it installs ok |
Close via #1975 |
thanks! Actually noticed i meant to complain about tidyr::drop_na, not r-spatial. But still nice something got fixed... |
Could you please add
{tidyr}
'sdrop_na
function to the tidyverse function for sf?I know it's just a wrapper for
filter(is.na(.))
but it's a convenient one :) and also for people who use it all the time it's always a source of error becausedrop_na
currently drops the geometry silently.Thanks!
The text was updated successfully, but these errors were encountered: