Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
edited by

I just want to remove a row based on a column value, but have not been able to do it for a "list" typeof. I am showing you the tibble that is build on a shapefile for the classification. 

The geometry column is also a bit different in the R dataframe. The geometry column should read with a c (as in vector). i.e. 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... But I don't know how o do it.

# 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

1 Answer

0 votes
by (107k points)

In R programming, you can take help of map/filter and with that you can check for the NaN values, where you can loop over the list column 'geometry with map, and it will 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 you can also use:

df1 %>%

     st_is_empty(.) %>%

     `!` %>%

     magrittr::extract(df1, ., )

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...