statascraper is an R package that extracts data from Stata log files
Switch branches/tags
Nothing to show
Clone or download
Latest commit c02cf81 Oct 28, 2016
Permalink
Failed to load latest commit information.
R Add function to read regression output. Mar 21, 2016
demo Added linebreak Oct 27, 2015
inst/example Added demo Oct 27, 2015
man Updated documentation Oct 27, 2015
.gitignore Updated .gitignore Oct 27, 2015
.travis.yml Updated .travis.yml Oct 27, 2015
DESCRIPTION Update DESCRIPTION Oct 28, 2016
NAMESPACE Re-organized R functions Oct 27, 2015
README.md Update README.md Jun 23, 2016

README.md

statascraper Build Status

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.

Installation

source("http://jtilly.io/install_github/install_github.R")
install_github("jtilly/statascraper")

Example

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.