Accessing level values (answer codes) in R from Limesurvey export

More
3 years 1 month ago - 3 years 1 month ago #123935 by jozilla
jozilla created the topic: Accessing level values (answer codes) in R from Limesurvey export
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 (e.g., ALB) as the Limesurvey answer code and the country name as the label (e.g. Albania).

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" 

Again no ISO codes. It would be great if anyone could tell me how to access these.

P.S.: also posted this on Stackoverflow .
Last Edit: 3 years 1 month ago by jozilla. Reason: quick fix, extra info.

Please Log in or Create an account to join the conversation.

More
3 years 1 month ago #123946 by holch
holch replied the topic: Accessing level values (answer codes) in R from Limesurvey export
isn't this rather an R question than a Limesurvey question? You might get some responses here, no doubt. There might be some R users here. But in Forum focusing on R, I think you are better off.

Because if I understand you right, Limesuvey exports the data fine (both ISO code and the country label), but you just can access the label, and not the ISO code).

I'm not a LimeSurvey GmbH member. I answer at the LimeSurvey forum in my spare time. No support via private message.
Some helpful links: Manual (EN) | Question Types | Workarounds

Please Log in or Create an account to join the conversation.

More
3 years 3 weeks ago #124166 by THO
THO replied the topic: Accessing level values (answer codes) in R from Limesurvey export
I think the question is a limeSurvey question and a R question at the same time and here should be some people using this R export.

Technically, however, this really is an R problem and is already discussed on stackoverflow - e.g. stackoverflow.com/questions/5869539/conf...ls-and-factor-labels

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.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!