0

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

smci
  • 32,567
  • 20
  • 113
  • 146
Rhonda
  • 1,661
  • 5
  • 31
  • 65

3 Answers3

4

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

RHertel
  • 23,412
  • 5
  • 38
  • 64
  • To get the boundaries, you can do `library(raster); g <- getData('GADM', level=1, country='CHE')` – Robert Hijmans Jun 22 '15 at 20:28
  • @RobertH When I attempt this I get `Error in substr(path, nchar(path), nchar(path)) : 4 arguments passed to .Internal(nchar) which requires 3` – Rhonda Jun 23 '15 at 12:50
  • @RobertH I upgrade to R 3.2.1, and now it processes. Trying to determine how to display the value of `g`, to determine the boundaries – Rhonda Jun 23 '15 at 13:39
  • @Sohni Mahiwal to display g, type `g` for metadata and `plot(g)` to see the boundaries – Robert Hijmans Jun 23 '15 at 20:19
1

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.

Shiva
  • 789
  • 6
  • 15
1

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')
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63