This function can be used to import files exported by LimeSurvey, a powerful Open Source online survey application that can be used for, for example, psychological experiments and other research.
importLimeSurveyData(datafile = NULL, scriptfile = NULL, limeSurveyRegEx.varNames = "names\\(data\\)\\[\\d*\\] <- ",="" limesurveyregex.tochar="data\\[, \\d*\\] <- as.character\\(data\\[, \\d*\\]\\)" ,="" limesurveyregex.varlabels="attributes\\(data\\)\\$variable.labels\\[\\d*\\] <- \" .*\"",="" limesurveyregex.tofactor="paste0("data\\[," \\d*\\]="" <-="" factor\\(data\\[,="" \\d*\\],="" "levels="c\\(.*\\),labels=c\\(.*\\)\\)")," limesurveyregex.varnamesanitizing="list(list(pattern = "#"," replacement =" ".")," list(pattern =" "\\$"," setvarnames =" TRUE," setlabels =" TRUE," converttocharacter =" FALSE," converttofactor =" FALSE," categoricalquestions =" NULL," massconverttonumeric =" TRUE," datahasvarnames =" TRUE," encoding =" "NULL"," dataencoding =" "unknown"," scriptencoding =" "ASCII")->
- The path and filename of the file containing the data (comma separated values).
- The path and filename of the file containing the R script to import the data.
- The regular expression used to extract the variable names from the script file. The first regex expression (i.e. the first expression between parentheses) will be extracted as variable name.
- The regular expression to detect the lines in the import script where variables are converted to the character type.
- The regular expression used to detect the lines in the import script where variable labels are set.
- The regular expression used to detect the lines in the import script where vectors are converted to factors.
- A list of regular expression patterns and their replacements to sanitize the variable names (e.g. replace hashes/pound signs ('#') by something that is not considered the comment symbol by R).
- setVarNames, setLabels, convertToCharacter, convertToFactor
- Whether to set variable names or labels, or convert to character or factor, using the code isolated using the specified regular expression.
- Which variables (specified using LimeSurvey variable names) are considered categorical questions; for these, the script to convert the variables to factors, as extracted from the LimeSurvey import file, is applied.
Whether to convert all variables to numeric using
- Whether the variable names are included as header (first line) in the comma separated values file (data file).
- encoding, dataEncoding, scriptEncoding
The encoding of the files;
scriptEncoding, and so can be used to specify the same encoding for both.
This function was intended to make importing data from LimeSurvey a bit easier. The default settings used by LimeSurvey are not always convenient, and this function provides a bit more control.
## Not run: # ### Of course, you need valid LimeSurvey files. This is an example of # ### what you'd do if you have them, assuming you specified that path # ### containing the data in 'dataPath', the name of the datafile in # ### 'dataFileName', the name of the script file in 'dataLoadScriptName', # ### and that you only want variables 'informedConsent', 'gender', 'hasJob', # ### 'currentEducation', 'prevEducation', and 'country' to be converted to # ### factors. # dat <- importLimeSurveyData(datafile = file.path(dataPath, dataFileName), # scriptfile = file.path(dataPath, dataLoadScriptName), # categoricalQuestions = c('informedConsent', # 'gender', # 'hasJob', # 'currentEducation', # 'prevEducation', # 'country')); # ## End(Not run)