1

I want to remove NAs from "SpatialPolygonsDataFrame". Traditional df approach and subsetting (mentioned above) does not work here, because it is a different type of a df. I tried to remove NAs as for traditional df and failed. The firsta answer, which also good for traditional df, does not work for spatial. I combine csv and a shape file below

countries <- readOGR(".","ne_50m_admin_0_countries")

I receive

class(data_pg_df)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

When I try to create a simple leaflet map, NAs create a problem.

[1] NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA    
[13] NA     NA     NA     NA     NA     NA     NA     NA     NA     "SSA"  "SSA"  NA    
[25] NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA     NA 

Map

I tried to remove NAs with sp.na.omit(), but receive an error

Error in sp.na.omit(data_pg_df) : could not find function "sp.na.omit"

Shape also does not work. My goal is remove NAs and have a clean map with polygons

Map 2

Thanks!

Community
  • 1
  • 1
Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • 2
    `Error ... could not find function "sp.na.omit"` is telling you [either you didn't load `spatialEco` or not a recent version containing `sp.na.omit`](https://www.rdocumentation.org/packages/spatialEco/versions/1.1-0/topics/sp.na.omit). See [this](https://stackoverflow.com/questions/7027288/error-could-not-find-function-in-r). Please fix that and update the question. – smci Sep 05 '18 at 01:14
  • Possible duplicate of [Remove rows with all or some NAs (missing values) in data.frame](https://stackoverflow.com/questions/4862178/remove-rows-with-all-or-some-nas-missing-values-in-data-frame) – smci Sep 05 '18 at 01:17
  • It is not a duplicate, because I have spatial df, not just df. Plus I checked this question, but it did not resolve my problem. Thanks. – Anakin Skywalker Sep 05 '18 at 03:51
  • 1
    In order to duplicate your results and develop a solution, please post a subset of your data source or a link to it. – J. Win. Sep 05 '18 at 06:37
  • 'spatial df' is merely a type of df, and the common issue is that some or all columns are NAs, I don't see that this is any different. Please post your dataframe: `dput(head(df))`. – smci Sep 05 '18 at 13:35
  • @smci, I resolved the issue, subsetting my df, not spatial df, thanks for your advice. – Anakin Skywalker Sep 07 '18 at 16:08

1 Answers1

2

have you loaded the 'spatialEco' library? after you loaded the library, try this

df <- sp.na.omit(data_pg_df, margin = 1)
  • 1
    > data_pg_df %>% drop_na() Error in UseMethod("drop_na_") : no applicable method for 'drop_na_' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector', 'SpatialPolygonsNULL')" > data_pg_df <- data_pg_df[apply(data_pg_df, 2, function(x){any(is.na(x))}),] Error in as.vector(data) : no method for coercing this S4 class to a vector – Anakin Skywalker Sep 05 '18 at 03:33
  • okay never work with spatial data before, I did some searching then edited my answer, check it out – Rendy Eza Putra Sep 05 '18 at 06:32
  • 1
    Rendy, I got it partially with your help. But margin should be 1, not 2, I need to delete NAs from rows. Plus I receive too many deletes and now my map looks exactly like image 2, but all colored country polygons disappeared. – Anakin Skywalker Sep 05 '18 at 13:25
  • ok, good. I didn't know the dataframe. So, I assumed the margin would be 2. – Rendy Eza Putra Sep 06 '18 at 04:40