R Package that extracts tables from Stata logfiles
This package scrapes Stata logfiles, looks for tables generated by tab
, tabstat
, list
, reg
, and areg
and then returns these tables as R matrices or stores them as comma separated value files. This package can be useful, when researchers (have to) use Stata to work with data sets but want to use R to analyze and display results.
source("http://jtilly.io/install_github/install_github.R")
install_github("jtilly/statascraper")
Suppose you have the following Stata do-file:
log using "test.log", replace
webuse auto.dta
di "tab2r tab.RData"
tab make foreign
log close
This do-file produces a logfile that looks like this:
. di "tab2r tab.RData"
tab2r tab.RData
. tab make foreign
| Car type
Make and Model | Domestic Foreign | Total
-------------------+----------------------+----------
AMC Concord | 1 0 | 1
AMC Pacer | 1 0 | 1
AMC Spirit | 1 0 | 1
Audi 5000 | 0 1 | 1
Audi Fox | 0 1 | 1
BMW 320i | 0 1 | 1
Buick Century | 1 0 | 1
...
We now use statascraper
to access the tabulated output from R. Note that we use Stata's display command di
to place markers in the logfile that tell statascraper
which tables to scrape and where to store the resulting RData or csv files. For instance, tab2r tab.RData
tells the scraper to scrape the results from tab and store them in the file tab.RData
.
library("statascraper")
# read tab commands
read.tab(filename = "test.log", outdir = ".", RData = TRUE, tag = "tab2r")
#> [1] "Created tab.RData"
head(load.RData("tab.RData"))
#> Domestic Foreign Total
#> AMC Concord 1 0 1
#> AMC Pacer 1 0 1
#> AMC Spirit 1 0 1
#> Audi 5000 0 1 1
#> Audi Fox 0 1 1
#> BMW 320i 0 1 1
A complete example can be found here with corresponding Stata do-file and Stata logfile.