Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export labels to R #101

Closed
tinok opened this issue Oct 20, 2015 · 5 comments
Closed

Export labels to R #101

tinok opened this issue Oct 20, 2015 · 5 comments

Comments

@tinok
Copy link
Member

tinok commented Oct 20, 2015

A user should be able to export a syntax file for reading their dataset using labels, just like they can currently with SPS files in SPSS.

@Edouard-Legoupil
Copy link

In order to provide an example of syntax comparison:

.sps value woudl be following for a 4 questions survey that would include diverse questions such as one free text, one date, one select, one integer question and one yes/no question encoded with integer:

_survey123.sps_

VARIABLE LABELS
    dG_AEnumerator "Assessor name"
    dG_ADate "Date of assessment"
    dG_AOrganisation_name "Organisation name"
    dG_Anumber "Number of person"
    dG_like "Did you like it?"

VALUE LABELS
    dG_AOrganisation_name
        'IRC' "IRC"          'other' "Other"        'unhcr' "UNHCR"
    dG_like
        '1' "yes"          '2' "No"      

in R the equivalent syntax file would be _survey123_R_syntax.R_:

data <- read.table("survey123_R_data_file.csv", sep=",", quote = "'", na.strings=c("","\"\""), stringsAsFactors=FALSE)

data[, 1] <- as.character(data[, 1])
attributes(data)$variable.labels[1] <- "Assessor name"
names(data)[1] <- "dG_AEnumerator"

## Depending on the date format style
data[, 2] <- as.date()<- as.Date(as.character(data[, 2]), "%d-%m-%Y") 
attributes(data)$variable.labels[2] <- "Date of assessment"
names(data)[2] <- "dG_ADate"

data[, 3] <- as.character(data[, 3)
attributes(data)$variable.labels[3] <- "Organisation name"
data[, 3] <- factor(data[, 3], levels=c( "IRC", "other",  "unhcr"),labels=c( "IRC","Other","UNHCR"))
names(data)[3] <- "dG_AOrganisation_name"

data[, 4] <- as.numeric(data[, 4])
attributes(data)$variable.labels[4] <- "Number of person"
names(data)[4] <- "dG_Anumber"

data[, 5] <- as.numeric(data[, 5])
attributes(data)$variable.labels[5] <- "Did you like it?"
data[, 5] <- factor(data[, 5], levels=c( "1", "2"),labels=c( "Yes","No"))
names(data)[5] <- "dG_like"

Both Integer and Numeric can be imported as numeric.
geopoint can be imported as character. It's not difficult to parse and extract the coordinates...
for instance, depending on the constraint on GPS recording accuracy, a certain number of digit after . will be recorded

data[,6] <- as.character(data[,6])
options(digits = 15)
data$lat <- as.numeric(substr(data[,6] , 1,13))
data$long <- as.numeric(substr(data[,6], 15,27))

@tinok
Copy link
Member Author

tinok commented Oct 20, 2015

Thanks! I propose we separate this issue into two parts: One to take care of labeling variables and options, and one for taking care of all the import steps from CSV. (The SPSS syntax file doesn't cover the import steps from CSV either at this point.)

@Edouard-Legoupil Are there any restrictions regarding the length of variable labels and option labels? Do non-ASCII characters need to be escaped?

@Edouard-Legoupil
Copy link

To my knowledge, there's no restrictions for label length. Note that the variable has two distinct attributes (name & label) and that there is a limit for name length: 10,000 bytes (256 bytes in versions of R before 2.13.0).

In SPSS, name length limit is 64 bytes (8 bytes in previous versions).

@Edouard-Legoupil
Copy link

Edouard-Legoupil commented Aug 29, 2017

FYI -- I have implemented those functions in a R package: https://github.com/Edouard-Legoupil/koboloadeR - you might just close this issue

@tinok
Copy link
Member Author

tinok commented Aug 30, 2017

Thanks so much @Edouard-Legoupil. We'll add a support page for this, pointing to your repo. Closing this issue now.

@tinok tinok closed this as completed Aug 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants