Skip to content
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

Closed
gregleleu opened this issue Jan 14, 2021 · 10 comments
Closed

Tidyverse function: drop_na #1578

gregleleu opened this issue Jan 14, 2021 · 10 comments
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!

Comments

@gregleleu
Copy link
Contributor

Could you please add {tidyr}'s drop_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 because drop_na currently drops the geometry silently.

Thanks!

@edzer edzer added feature a feature request or enhancement help wanted ❤️ we'd love your help! labels Jan 14, 2021
@KiranmayiV
Copy link

KiranmayiV commented Jan 15, 2021

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)

@edzer
Copy link
Member

edzer commented Jan 15, 2021

It looks like the class is dropped.

@KiranmayiV
Copy link

It looks like the class is dropped.

Aaah, ok thanks. I will look into it.

@KiranmayiV
Copy link

If this looks good for drop_na.sf, I can put in a PR to include this in tidyverse.R . Please let me know if you prefer any changes.

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,

@edzer
Copy link
Member

edzer commented Jan 18, 2021

No doubt this will mostly work, but I'd appreciate if you'd follow the pattern e.g. given by arrange.sf, as that would also catch some corner cases (e.g. multiple geometry columns) that your solution doesn't - or else point out why you divert.

@KiranmayiV
Copy link

Sorry about that, I am new to open source contribution and learning. Yes, happy to include for corner cases - following arrange.sf pattern.

@ilikegitlab
Copy link

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....

@gregleleu
Copy link
Contributor Author

gregleleu commented Jul 17, 2022

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

@gregleleu
Copy link
Contributor Author

Close via #1975

@ilikegitlab
Copy link

thanks! Actually noticed i meant to complain about tidyr::drop_na, not r-spatial. But still nice something got fixed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

4 participants