rdataframejoinnaspatial-data-frame

Join two columns into one in a SpatialDataFrame, but omitting NAs In R


I'm trying to join two columns of a SpatialDataFrame (shapefile) into one using the R program, but in both columns there are empty spaces, when they are together with the name plus NA, however I would like the NAs not to appear in my new column. I used the paste function. something like this:

  This is the structure of my SpatialDataFrame:


  ID           city                city2
1  1      saõ paulo                 <NA>
2  2      Rio de Janeiro            <NA>
3  3           <NA>            Belo Horizonte
4  4           <NA>            Curitiba

obs. my original data is not this and has more columns

I used this:

data$newCity <- paste(data$city, data$city2) # I don't want to show in my data Na

1.

ID          city          city2                newCity
  1      saõ paulo         <NA>            saõ paulo NA
  2  Rio de Janeiro        <NA>            Rio de Janeiro NA
  3        <NA>       Belo Horizonte       NA Belo Horizonte
  4        <NA>       Curitiba             NA Curitiba

In fact this would be the desired result:

ID          city          city2                 newCity
 1      saõ paulo         <NA>                saõ paulo
 2    Rio de Janeiro      <NA>               Rio de Janeiro
 3        <NA>         Belo Horizonte         Belo Horizonte
 4        <NA>          Curitiba              Curitiba

Solution

  • You can use the function coalesce from dplyr package:

    df <- data.frame(ID = 1:4,
                     city = c("sao paulo", "rio de janeiro", NA, NA),
                     city2 = c(NA, NA, "Belo Horizonte", "Curitiba"), stringsAsFactors = FALSE)
    
    
    library(dplyr)
    df %>% mutate(City = coalesce(city, city2))
      ID           city          city2           City
    1  1      sao paulo           <NA>      sao paulo
    2  2 rio de janeiro           <NA> rio de janeiro
    3  3           <NA> Belo Horizonte Belo Horizonte
    4  4           <NA>       Curitiba       Curitiba