Convert a specially written text file with information on variable labels and value labels into R code that converts integer vectors into factor variables.
labels2R(lfile, rfile, dfname = "b", echo = FALSE)The path to the text file to be converted.
The path to the file to be created.
Name of data.frame where the variables are.
If TRUE, then lines of lfile are printed in the R Console
while the file is parsed. This may be useful debugging.
NULL.
The return value is NULL, but rfile is created if the function is
successful. The file is an R code that converts numeric vectors into
factors. The text file must have a format as in the example below:
v1 Sex 1 Female 2 Malev2 Household income
v3 Taking all things together, would you say you are... 1 Very happy 2 Rather happy 3 Not very happy 4 Not at all happy
The above code would be converted into:
b$v1 <- factor(b$v1, levels=c(1, 2), labels=c("Female", "Male"))
attr(b$v1, "label") <- "Sex"
attr(b$v2, "label") <- "Household income"
b$v3 <- factor(b$v3, levels=c(1, 2, 3, 4),
labels=c("Very happy", "Rather happy",
"Not very happy", "Not at all happy"))
attr(b$v3, "label") <- "Taking all things together, would you say you are..."