Additonal Tools for Converting Swiss Postcodes to Cantons

I have previously presented a simple set of tools to convert Swiss postcodes to cantons. Here are a two really simple tools I use in conjunction with the code posted previously.

The first one is a simple wrapper for the converters I presented last time. It converts cantonal identity numbers to abbreviated labels and vice versa.

convid <- function(ID) {
clabels <- c("ZH", "BE", "LU", "UR", "SZ", "OW", "NW", "GL", "ZG", "FR", "SO", "BS","BL", "SH", "AR", "AI", "SG", "GR", "AG", "TG", "TI", "VD", "VS", "NE", "GE", "JU")
id <- ifelse(is.numeric(ID), clabels[ID], match(ID, clabels))
return(id)
}

The second one checks whether a number is a valid Swiss postcode, as in whether it is a postcode actually in use.

plzvalid <- function(PLZ) {
return(!is.na(plzcanton(PLZ)))
# usage: which(!sapply(1:1008, function(x) plzvalid(plz[x])))
}

All code also on Gist. I haven’t finished my code to convert postcodes to local language, though, as I ended up doing something else…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: