Home > Software design >  Subset a large spatial dataframe based on certain counties
Subset a large spatial dataframe based on certain counties

Time:07-24

Im trying to subset a large spatial dataframe based on certain counties but I still get the same dataframe after subseting.

library(raster)
library(leaflet)
library(viridis)

# Get USA polygon data
USA <- getData("GADM", country = "usa", level = 2)

# Prepare data
subusa<-subset(USA,"Cheyenne"%in%USA$NAME_2&"Kimball"%in%USA$NAME_2)

CodePudding user response:

I think you need

subusa<-subset(USA,"Cheyenne" == USA$NAME_2 | "Kimball"== USA$NAME_2)

This should retrieve rows where NAME_2 is Cheyenne or Kimball.

I think the original "Cheyenne"%in%USA$NAME&"Kimball"%in%USA$NAME resolves to TRUE&TRUE which equals a single logical value TRUE, which then gets recycled over every row of the data frame and returns all rows.

  •  Tags:  
  • r
  • Related