0

I'm analysing some data from a Limesurvey questionnaire in R. One of the survey questions asks people for their country of origin and uses a dropdown with a list of countries. I've created a labelset for this with the 3-letter ISO country code as the Limesurvey answer code and the country name as the label:

Labelset with ISO codes for countries

When exporting my Limesurvey data to R, both the codes and labels are saved into the R syntax file. The codes are saved as the 'level' for the factor, and the label is of course used as the label (abbreviated for clarity):

data[, 7] <- as.character(data[, 7])
attributes(data)$variable.labels[7] <- "Which country are you from?"
data[, 7] <- factor(data[, 7], levels=c("AFG","ALA","ALB", ..., "ZMB","ZWE"),labels=c("Afghanistan","Åland Islands","Albania", ..., "Zambia","Zimbabwe"))
names(data)[7] <- "Q3_Nationality"

However, I cannot figure out how to access this 3-letter ISO code in R? It would be great to get the ISO code, so I can feed that into the rworldmap joinCountryData2Map() function and plot data on a map. (I know that I can also just pass the country name to that function, but that's more error prone.)

If I examine the variable, I get this:

> data$Q3_Nationality
  [1] Guyana
  249 Levels: Afghanistan Åland Islands Albania Algeria ... Zimbabwe

Similarly, levels() just gives me:

levels(data$Q3_Nationality)
  [1] "Afghanistan"                                 
  [2] "Åland Islands"                               
  [3] "Albania"                                     
  [4] "Algeria"                                     
  [5] "American Samoa" 

No sign of the "AFG", "ALA", "ALB" ISO answer codes.

Trying to convert it to a character, gives me the full label again:

as.character(levels(data$Q3_Nationality))
  [1] "Afghanistan"                                 
  [2] "Åland Islands"                               
  [3] "Albania"                                     
  [4] "Algeria"                                     
  [5] "American Samoa" 

It would be great if anyone could tell me how to access these!

divibisan
  • 11,659
  • 11
  • 40
  • 58
jozilla
  • 36
  • 4

1 Answers1

0

I would propose a dirty solution in that way, that you just edit your R-Syntax-file and set the levels as labels as well. However, if you want to use the full names for the countries later on for printing or plotting, you have to transform it then. You can use the code syntax file in your code, maybe on a copy for printing.

BumbleBee
  • 986
  • 9
  • 19