Home > other >  Filtering lists in column that are not empty
Filtering lists in column that are not empty

Time:11-21

I have a column called "geo" where I store many lists of coordinates, each list representing a bounding box. The problem is that I have other elements in the geo column that I want to remove.

How can I just keep the rows where a list of coordinates is available? and how can I store these coordinates in a separate column? see table

EDIT: this is the dput()

structure(list(full_name = c("Karlsruhe, Deutschland", "Karlsruhe, Deutschland", 
"Karlsruhe, Deutschland", "Volksparkstadion", "Volksparkstadion", 
"Volksparkstadion"), country_code = c("DE", "DE", "DE", "DE", 
"DE", "DE"), place_type = c("city", "city", "city", "poi", "poi", 
"poi"), name = c("Karlsruhe", "Karlsruhe", "Karlsruhe", "Volksparkstadion", 
"Volksparkstadion", "Volksparkstadion"), country = c("Deutschland", 
"Deutschland", "Deutschland", "Deutschland", "Deutschland", "Deutschland"
), id = c("5b146bf0b819f1af", "5b146bf0b819f1af", "5b146bf0b819f1af", 
"0fc3a3fa5494c000", "0fc3a3fa5494c000", "0fc3a3fa5494c000"), 
    geo = list(type = "Feature", bbox = list(8.2773, 48.9405, 
        8.5418, 49.0914), properties = structure(list(), .Names = character(0)), 
        type = "Feature", bbox = list(9.8986, 53.5871, 9.8986, 
            53.5871), properties = structure(list(), .Names = character(0)))), row.names = c(NA, 
6L), class = "data.frame")





CodePudding user response:

Based on the structure, perhaps

subset(df1, lengths(geo) > 0 & names(geo) != "type")

-output

               full_name country_code place_type             name     country               id                              geo
2 Karlsruhe, Deutschland           DE       city        Karlsruhe Deutschland 5b146bf0b819f1af 8.2773, 48.9405, 8.5418, 49.0914
5       Volksparkstadion           DE        poi Volksparkstadion Deutschland 0fc3a3fa5494c000 9.8986, 53.5871, 9.8986, 53.5871
  • Related