在 SpatialDataFrame 中将两列合并为一列,但在 R 中省略 NA

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

我正在尝试使用 R 程序将 SpatialDataFrame(shapefile)的两列合并为一列,但是当它们与名称加 NA 一起时,两列中都有空格,但是我想要NA 不会出现在我的新专栏中。我用的是粘贴功能。 像这样:

  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

观察。我的原始数据不是这个并且有更多列

我用过这个:

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

事实上,这将是期望的结果:

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

使用 paste 将字符列粘合在一起,由 space 分隔,即“_”。试试这个:

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

您可以使用 dplyr 包中的函数 coalesce

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

您可以在 tidyr 中使用 unite():

library(tidyr)

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

参数na.rm = T 仅适用于字符列。

另一个 base R 选项可以是:

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

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