0

I am trying to replace the word "United States" with "USA" to colour a choropleth map for a project. This is the code I am using on my data frame:

country_data_map$region[country_data_map$region == "United States"] <- "USA"

tail(country_data_map,20)

but it still prints out

    region  value
<chr>   <chr>
208 Turkmenistan    73,08
209 Turks & Caicos Is       15,67
210 Tuvalu  20,03
211 Uganda  67,83
212 Ukraine 20,34
213 United Arab Emirates    14,51
214 United Kingdom  5,16
215 United States   6,5
216 Uruguay 11,95
217 Uzbekistan  71,1
218 Vanuatu 55,16
219 Venezuela   22,2
220 Vietnam 25,95
221 Virgin Islands  8,03
222 Wallis and Futuna   
223 West Bank   19,62
224 Western Sahara  
225 Yemen   61,5
226 Zambia  88,29
227 Zimbabwe    67,69

Does anyone know what I am doing wrong?

2
  • Could you share some actual data, like tail(country_data_map,20). The issue could be a number of things (extra spaces, for example), and a snippet of data would help us copy and paste your data into R and test some things. Commented Feb 17, 2021 at 16:46
  • You are doing nothing wrong as far as what you're showing. I can build a data frame with sample data and the conversion is successful using your code. Commented Feb 17, 2021 at 16:50

1 Answer 1

1

If you're having trouble with sneaky spaces, you could do:

country_data_map$region[stringr::str_detect(country_data_map$region, "United States")] <- "USA"

And that will still work if the value is " United States" or "United States " or something like that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.