Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

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

  • jozilla
  • jozilla's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 8 months ago - 8 years 8 months ago #123935 by jozilla
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):
Code:
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:
Code:
> data$Q3_Nationality
  [1] Guyana
  249 Levels: Afghanistan Åland Islands Albania Algeria ... Zimbabwe

Similarly, levels() just gives me:
Code:
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:
Code:
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: 8 years 8 months ago by jozilla. Reason: quick fix, extra info.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 7 months ago #123946 by holch
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 answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
More
8 years 7 months ago #124166 by THO
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.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose