0

I am working with the maps package in R and I am having trouble calling states which have spaces in their names, since the data I am using lists each state without spaces. For example:

map("state", popdata[26,1], boundary = F, fill = T, col = 5, add = T)

where popdata[26,1] is NewHampshire

Gives me an error since the command requires the state to be denoted as New Hampshire (with the space).

Is there a way around this or a simple way to go through the data and add the appropriate spaces?

Evan Prickett
  • 11
  • 1
  • 2
  • just a little searching and you would have found this, which should help: http://stackoverflow.com/questions/26896971/add-space-between-two-letters-in-a-string-in-r – grrgrrbla Jun 11 '15 at 16:56

1 Answers1

0

If you are certain that there will be a capital letter where the space should have been, use:

> st = c("NewHampshire","Illinois")
> gsub("([a-z])([A-Z])","\\1 \\2",st)
[1] "New Hampshire" "Illinois" 
Alexey Ferapontov
  • 5,029
  • 4
  • 22
  • 39