• Export User's Guide
    • Introduction to Export Data
    • How to ....
    • Loading Exported Data Into ...
      • R
      • SAS
      • SPSS
      • Stata
    • Summaries

Load Exported Data Into R

When you choose R as the export format, CSPro creates two files: a data file (.DAT) and a R script file (.R). To load the data into R, select File -> Open script.
Select the .R file and then the R Editor will display the contents of the script file. To run the script file, select Edit -> Run all.
The script file will add a data frame containing the variables exported from the CSPro file. The name of the data frame will be the same as the name of the data file. If you exported variables from multiple records, you may have more than one data frame, one for each record. The items from the record will be added to the data frame as vectors.
Your R script file will look similar to the following:
cspro.factor.type = 1
cspro.factor.create.new.variable = FALSE
 
# CSPro Export Factor Options:
    # cspro.factor.type (0): do not use factors
    # cspro.factor.type (1): factor only discrete numeric variables
    # cspro.factor.type (2): factor both discrete numeric and alpha variables
 
    # cspro.factor.create.new.variable: TRUE to add the factored variables as separate variables
 
exported <- read.fortran("C:/MySurvey/Exported.dat",c("I1","I1"))
 
names(exported) <- c("relationship","sex")
 
if( cspro.factor.type != 0 )
{
 
    if( cspro.factor.create.new.variable ) exported$relationship.f <- factor(exported$relationship,levels = c(1,2,3,4,5),labels = c("Head","Spouse","Child","Other Relative","Non-Relative"))
    else exported$relationship <- factor(exported$relationship,levels = c(1,2,3,4,5),labels = c("Head","Spouse","Child","Other Relative","Non-Relative"))
 
    if( cspro.factor.create.new.variable ) exported$sex.f <- factor(exported$sex,levels = c(1,2),labels = c("Male","Female"))
    else exported$sex <- factor(exported$sex,levels = c(1,2),labels = c("Male","Female"))
 
}
 
rm(cspro.factor.type)
rm(cspro.factor.create.new.variable)
The two variables at the top give you options to control whether or not you want to use factors with your vectors. The comments give information about each of the options. If creating new vectors for the factors, the name of the new vector will be the original name followed by .f. The default option is to modify the original vector to add factors for discrete numeric variables. Make any changes to these two options before running the script file.
Adding factors facilitates the use of value set labels in R output. For example, without factors, a table in R might look like this:
> table(exported$relationship,exported$sex)
 
        1     2
  1 30833  4351
  2   108 30371
  3 30933 21894
  4  5610 11860
  5   241   476
Using factors results in a more readable table:
> table(exported$relationship.f,exported$sex.f)
 
                  Male Female
  Head           30833   4351
  Spouse           108  30371
  Child          30933  21894
  Other Relative  5610  11860
  Non-Relative     241    476