Question

I would like to move from STATA to R but I'm facing a challenge integrating variable labels and lookup values in my data. In STATA for each of my text data files (.dat) I have a dictionary file (.dct) defining the type of variables in the .dat and a script file (.do) where I can integrate the Variable names and lookup values into a final binary data file. For example:

infix using "otherincome.dct"

label variable HH_ID "Description of HH_ID"
label variable record_id "Description of record_id"
label variable income "Description of income"
label variable inctimes "Description of inctimes"
label variable incperiod "Description of incperiod"
label variable incsea "Description of incsea"
label variable incamnt "Description of incamnt"
label variable incgender "Description of incgender"
label variable incnotes "Description of incnotes"
label variable incfactor "Description of incfactor"

#delimit ;
label define LBL40
  -999 "Not clear in paper"
  -888 "Empty"
  -777 "Other"
  1 "Male"
  2 "Female"
  4 "Jointly"
;
label define LBL41
  -999 "Not clear in paper"
  -888 "Empty"
  -777 "Other"
  1 "Remittances"
  2 "Bussines"
  3 "Employment"
  4 "Savings"
;
label define LBL42
  -999 "Not clear in paper"
  -888 "Empty"
  -777 "Other"
  1 "Hour"
  2 "Day"
  3 "Month"
  4 "Year"
;
label define LBL43
  -999 "Not clear in paper"
  -888 "Empty"
  -777 "Other"
  1 "Long rains"
  2 "Short rains"
  3 "Dry spell"
  4 "All seasons"
;

#delimit cr
label values incgender LBL40
label values income LBL41
label values incperiod LBL42
label values incsea LBL43

/* Save data a binary data*/
save otherincome

Is if possible to do something like this in R?

Was it helpful?

Solution

You can't really assign a label to a regular variable in R. You could set a custom attribute or something to track the name and create custom print objects for your functions, but that might be overkill.

Some of your labels look like categorical variables, which in R are called factors. You set those up with

incgender <- factor(c(4,1,1,2,-999,1,-777), levels=c(-999,-888,-777,1,2,4), labels=c("Not clear in paper", "Empty","Other","Male","Female","Jointly"));
print(incgender);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top