0

I have an sf object data frame in R with a column that is a list of characters and character lists itself. For some rows the column is an empty list. I want to subset to only those rows where the column in question is an empty list.

Here's a screenshot of the data structure:

the sf object in question with

I have tried to subset it multiple ways but all have failed, returning a data.frame with 0 rows:

a <- character(0)
subset(dt, identical(a, neighborhood))
Simple feature collection with 0 features and 2 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS84(DD)
[1] POP20        geometry     neighborhood
<0 rows> (or 0-length row.names)

subset(dt, is_empty(neighborhood))
Simple feature collection with 0 features and 2 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS84(DD)
[1] POP20        geometry     neighborhood
<0 rows> (or 0-length row.names)

> subset(dt, length(neighborhood[[1]])==0)
Simple feature collection with 0 features and 2 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS84(DD)
[1] POP20        geometry     neighborhood
<0 rows> (or 0-length row.names)

subset(dt, is.na(neighborhood))
Simple feature collection with 0 features and 2 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS84(DD)
[1] POP20        geometry     neighborhood
<0 rows> (or 0-length row.names)

subset(dt, length(neighborhood[[1]])==0)
Simple feature collection with 0 features and 2 fields
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS84(DD)
[1] POP20        geometry     neighborhood
<0 rows> (or 0-length row.names)

I'm at a loss for subsetting this.

BLP92
  • 145
  • 2
  • 10
  • 1
    Empty lists should return length == 0 – IRTFM Mar 19 '23 at 00:56
  • 1
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. Your question is about data types, but we don't have access to them. It doesn't seem like this is actually specific to `sf` either. Best guess is you can subset by `lengths(neighborhood) == 0`. Note that it's length**s**, not length – camille Mar 19 '23 at 01:35

1 Answers1

3

First a small example:

test <- data.frame(
    "POP20" = 1:4,
    "neighb" = I(list("O'Hare", c("Garfield", "Cleaning"), character(0),  "O'hare"))
)

Then you just have to remember that length return length for a object. If you want the same for a list you should use lengths (<-- see the S).

subset(test, lengths(test$neighb) == 0)
defuneste
  • 376
  • 2
  • 7