Reputation: 241
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
Upvotes: 3
Views: 215
Reputation: 35624
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.
Upvotes: 1
Reputation: 40171
Another base R
option could be:
with(df, pmax(city, city2, na.rm = TRUE))
[1] "sao paulo" "rio de janeiro" "Belo Horizonte" "Curitiba"
Upvotes: 2
Reputation: 16178
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
Upvotes: 0
Reputation: 125727
Using paste
glues the character columns together, separated by a space, i.e. "_". Try this:
data$newCity <- ifelse(is.na(data$city), data$city2, data$city)
Upvotes: 1