15

Is it possible to call Stata functions from R?

asked Nov 12, 2010 at 16:45

3 Answers 3

13

Not directly, i.e. there is no package I am aware of that implements a bridge.

You can always call external programs using system() but that is neither elegant nor efficient. That said, you could prepare data in R, write it out, call Stata and then read the results in; see help(system).

Nick Cox
37.4k6 gold badges37 silver badges51 bronze badges
answered Nov 12, 2010 at 17:32
Sign up to request clarification or add additional context in comments.

3 Comments

This is probably the best solution. Stata has a set of command line tools that allow you to run a .do/.ado file from the shell (windows: stata.com/support/faqs/win/batch.html or unix/linux: fmwww.bc.edu/Gstat/docs/stataunixlinux.html#batch)
There is now an RStata package on GitHub that attempts to create a bridge between R and Stata.
@Thomas RStata deserves to be it's own answer.
12

There's now an RStata package on CRAN that bridges R and Stata.

answered Mar 5, 2016 at 14:44

1 Comment

This blog post provides a good introduction to RStata fsolt.org/blog/2018/08/15/switch-to-r.html
3

The real problem is that Stata doesn't have an interactive interpreter you can pass arguments to.

Dirk is right; you can just go ahead and write the data to a common format (if size is large and speed is an issue, fixed width is safe), but you can also just use .dta throughout the process, using read.dta in R and natively reading in Stata.

Also, in R you can call to the system() you can pass a do file or a string containing a bunch of Stata commands.

So, generally, trying to use Stata for this or that task may or may not be worth it, especially if an R equivalent is close by.

Nick Cox
37.4k6 gold badges37 silver badges51 bronze badges
answered Nov 21, 2010 at 10:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.