I have looked through a few other solutions on how to remove a row based on a column value, but have not been able to do it for a "list" typeof
. The following tibble is a shapefile built for classification training. To rasterize the shapefile, I need to remove rows that contain no geometry data. ie row 8. I can simply perform df <- df$geometry[-8, ]
, however that would be inefficient for large data sets.
The geometry column also reads a bit different in the R dataframe (for some reason, this is what the tibble
function outputted). The geometry column should read with a c
(as in vector). ie c(-123.1166, 44.67333)
. So the EMPTY
actually reads c(NaN, NaN)
.
I was thinking about turning the typeof
"list" into a "string" and removing it like that??? Any suggestions?
# A tibble: 102 x 3
Class Names2 geometry
<dbl> <fct> <POINT [°]>
1 1 Hole (-123.1166 44.67333)
2 1 Hole (-123.1166 44.67333)
3 1 Hole (-123.1166 44.67332)
4 1 Hole (-123.1167 44.6734)
5 1 Hole (-123.1167 44.6734)
6 1 Hole (-123.1166 44.67344)
7 1 Hole (-123.1165 44.67358)
8 1 Hole EMPTY
9 1 Hole (-123.1167 44.67367)
10 1 Hole (-123.1167 44.67367)
# ... with 92 more rows
Here is one option with map/filter
, where we loop over the list
column 'geometry with map
, check if not all
the values are NaN
, to return a logical vector in filter
to remove those with all NaN
values
library(dplyr)
library(purrr)
df1 %>%
filter(map_lgl(geometry, ~ !all(is.nan(.x)))
If it is an sf
object, an option is st_is_empty
library(sf)
df1 %>%
filter(!st_is_empty(geometry)]
Or it could be
df1 %>%
st_is_empty(.) %>%
`!` %>%
magrittr::extract(df1, ., )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.