Vector length mismatch in Swiss Language Map

369 views Asked by At

I am studying this website

and don't understand some things:

con <- url("http://biogeo.ucdavis.edu/data/gadm2/R/CHE_adm1.RData")
print(load(con))

Output is

[1] "gadm"

The code continues by closing the connection

close(con)

Then I execute

language <- c("german", "german", "german","german",
 "german","german","french", "french",
 "german","german","french", "french", 
 "german", "french","german","german",
 "german","german","german", "german",
 "german","italian","german","french",
 "french","german","german")

Honestly, I don't know how the person came up with this matrix, but then I get error

Error in `[[<-.data.frame`(`*tmp*`, name, value = c(2L, 2L, 2L, 2L, 2L,  : 
  replacement has 27 rows, data has 26

Please guide

3

There are 3 answers

4
RHertel On BEST ANSWER

It seems that one "french" entry should be removed from the list at the end of the third line. I don't know if this was a mistake in the example or a change in the data of the map (formerly at http://gadm.org/data/rda/CHE_adm1.RData , now at http://biogeo.ucdavis.edu/data/gadm2/R/CHE_adm1.RData). In any case I could reproduce the map shown on the website by using:

language <- c("german", "german", "german","german",
          "german","german","french", "french",
          "german","german","french",  
          "german", "french","german","german",
          "german","german","german", "german",
          "german","italian","german","french",
          "french","german","german")

enter image description here

0
Shiva On

If you look at the comments section in the link you provided, the author mentioned that he hard-coded the language vector. http://blog.revolutionanalytics.com/2009/10/geographic-maps-in-r.html

Regarding the error, it is pretty straight forward. It notifies that language vector has 27 entries while there are only 26 swiss language regions(this might be from gadm package AFAIK). So, try deleting one entry from the language vector.

0
Robert Hijmans On

RHertel solved the problem, but here is how I would approach it, perhaps useful as a background;

library(raster)
g <- getData('GADM', level=1, country='CHE')

# create a data.frame of cantons and language
# set them to German (a common one)
lang <- data.frame(g$NAME_1, lang='German')
lang

# now fix the entries that need to be French or Italian
# and merge back to g (a SpatialPolygonsDataFrame) 
g <- merge(g, lang, by='NAME_1')

spplot(g, 'lang')