Import STATA data with foreign (2)
Data can be very diverse, going from character vectors to categorical variables, dates and more. It's in these cases that the additional arguments of read.dta() will come in handy.
The arguments you will use most often are convert.dates, convert.factors, missing.type and convert.underscore. Their meaning is pretty straightforward, as Filip explained in the video. It's all about correctly converting STATA data to standard R data structures. Type ?read.dta to find out about about the default values.
The dataset for this exercise contains socio-economic measures and access to education for different individuals (Source: World Bank). This data is available as edequality.dta, which is located in the worldbank folder in your working directory.
This exercise is part of the course
Intermediate Importing Data in R
Exercise instructions
- Specify the path to the file using
file.path(). Call itpath. Remember the"edequality.dta"file is located in the"worldbank"folder. - Use the
pathvariable to import the data file in three different ways; each time show its structure withstr(): edu_equal_1: By passing only the filepathtoread.dta().edu_equal_2: By passing the filepath, and settingconvert.factorstoFALSE.edu_equal_3: By passing the filepath, and settingconvert.underscoretoTRUE.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# foreign is already loaded
# Specify the file path using file.path(): path
# Create and print structure of edu_equal_1
# Create and print structure of edu_equal_2
# Create and print structure of edu_equal_3