This lesson offers some more really short examples of importing data from STATA, SAS, and other formats into an R workspace.
In the past I have used an R package called foreign
to import STATA-brand .dta files into R. I recently discovered a packages called haven
from the tidyverse
that has methods for importing SAS and STATA data files.
library(haven)
library(here)
## here() starts at /Users/aaronmamula/Documents/R-Course-NOAA-HD-2020/rHD-skill-1-Data-Import
For this example I pulled this STATA .dta file from the UCLA Statistical Consulting website. I don't know much about it which is ok because it's only purpose here is to illustrate that STATA .dta files can be read into R using read_dta()
from the haven
package.
stata.example <- read_dta(here("data/stata-example.dta"))
head(stata.example)
## # A tibble: 6 x 4
## y s a b
## <dbl> <dbl> <dbl> <dbl>
## 1 7 1 1 1
## 2 14 1 2 2
## 3 12 1 3 3
## 4 3 2 1 1
## 5 5 2 3 2
## 6 11 2 2 3
For this portion I have pulled a SAS data file from the Principles of Econometrics website.
cocaine <- read_sas(here("data/cocaine.sas7bdat"))
head(cocaine)
## # A tibble: 6 x 4
## PRICE QUANT QUAL TREND
## <dbl> <dbl> <dbl> <dbl>
## 1 57.5 1000 62.5 1
## 2 77.2 454. 62.5 1
## 3 77.6 28.4 19 1
## 4 84.7 14.2 19 1
## 5 77.6 7.09 19 1
## 6 91.7 3.54 19 1