I have a data frame with columns of the city, state and country. I want to create a line that combines: "City, state, country." However, in one of my cities there is no state (there is instead NA). I want the line for this city to be "City, country." Here is the code that creates the wrong string:
city <- c("Austin", "Knoxville", "Salk Lake City", "Prague")
state <- c("Texas", "Tennessee", "Utah", NA)
country <- c("United States", "United States", "United States", "Czech Rep")
dff <- data.frame(city, state, country)
dff["string"] <- paste(city, state, country, sep=", ")
When I show dff$string, I get the following. Please note: the last line has NA,which is not needed:
> dff["string"]
string
1 Austin, Texas, United States
2 Knoxville, Tennessee, United States
3 Salk Lake City, Utah, United States
4 Prague, NA, Czech Rep
What should I do to skip this one NA,, including sep = ", ".