How do I use elements of a vector as arguments for another command in R?

27 views Asked by At

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?

1

There are 1 answers

0
Alexey Ferapontov On

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"