Import STATA data with haven
Next up are STATA data files; you can use read_dta()
for these.
When inspecting the result of the read_dta()
call, you will notice that one column will be imported as a labelled
vector, an R equivalent for the common data structure in other statistical environments. In order to effectively continue working on the data in R, it's best to change this data into a standard R class. To convert a variable of the class labelled
to a factor, you'll need haven
's as_factor()
function.
In this exercise, you will work with data on yearly import and export numbers of sugar, both in USD and in weight. The data can be found at: https://assets.datacamp.com/production/course_1478/datasets/trade.dta
This is a part of the course
“Intermediate Importing Data in R”
Exercise instructions
- Import the data file directly from the URL using
read_dta()
, and store it assugar
. - Print out the structure of
sugar
. TheDate
column has classlabelled
. - Convert the values in the
Date
column ofsugar
to dates, usingas.Date(as_factor(___))
. - Print out the structure of
sugar
once more. Looks better now?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# haven is already loaded
# Import the data from the URL: sugar
# Structure of sugar
# Convert values in Date column to dates
# Structure of sugar again