I need to subset a global fishing dataset to just have the fishing in the Pacific. Right now I have a shapefile of the Pacific and I am using the sp method of subsetting (see below)

oceans <- readOGR(dsn = ".", layer = "PacificOcean")
gfw = readRDS(filename2)
gfw <- SpatialPointsDataFrame(coords = gfwcoords, data = gfwdata,
                              proj4string = CRS(proj4string(oceans)))
gfwmask<- gfw[oceans, ]

This works just fine, BUT, I need to do it hundreds of times and it takes several minutes each time. Does anyone have suggestions for a faster method to subset a dataframe by a shapefile?

  • 1
    How big and complex are your objects? How detailed is your shape? How many rows in your data? You asked this 9 hours ago - if "several minutes" is "10", then you can do six every hour which means you could have done 54 by now - "hundreds" might take a few days but at least its done, and it might be quicker than waiting for an answer. Also, have you tried data and methods from sf pacakge?
    – Spacedman
    Sep 17, 2019 at 6:39
  • The data frames are a couple hundred thousand a piece and the shape goes around every single island, coastline, and archipelago in the Pacific so it's fairly detailed. I mispoke when I said hundreds above. I need to do it hundreds of times for a single analysis, but we are running this analysis dozens of times. So it comes out to more like thousands of times. And yes, I have it running the slow way while waiting for feedback here, so I'm still making slow and steady progress. Just trying to figure out a better way to do it for future analyses. I will look in to the sf package. Sep 17, 2019 at 14:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.