Thursday, February 24, 2011

How to read and write Stata data (.dta) files into R

Here's an R tutorial where I explain how to read Stata data files into R (even if you don't own the program Stata). I also offer some other basic tips.

Of note, you can also write Stata .dta files from R (if your coauthors or journals insist on having ".dta" data).

Suppose your R data frame has the name fuzzybunny and you want to save the file to the C: drive as myfuzzydata.dta. After loading the foreign library,


Here's the syntax:

write.dta(fuzzybunny, "C://myfuzzydata.dta")

Try it out and impress your friends (you know, the friends who only know how to use Stata).

1 comment:

  1. Thanks so much! I needed to quickly learn how to read in a Stata file and this did the trick.