简体   繁体   中英

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

Another base R option could be:

with(df, pmax(city, city2, na.rm = TRUE))

[1] "sao paulo"      "rio de janeiro" "Belo Horizonte" "Curitiba" 

Using paste glues the character columns together, separated by a space, ie "_". Try this:

data$newCity <- ifelse(is.na(data$city), data$city2, data$city)

You can use unite() in tidyr :

library(tidyr)

df %>%
  unite(newCity, city:city2, remove = F, na.rm = T)

The argument na.rm = T works only on character columns.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM