
Last chance! 50% off unlimited learning
Sale ends in
Currently haven can read and write logical, integer, numeric, character
and factors. See labelled
for how labelled variables in
Stata are handled in R.
read_dta(file, encoding = NULL)read_stata(file, encoding = NULL)
write_dta(data, path, version = 14)
Either a path to a file, a connection, or literal data (either a single string or a raw vector).
Files ending in .gz
, .bz2
, .xz
, or .zip
will
be automatically uncompressed. Files starting with http://
,
https://
, ftp://
, or ftps://
will be automatically
downloaded. Remote gz files can also be automatically downloaded &
decompressed.
Literal data is most useful for examples and tests. It must contain at least one new line to be recognised as data (instead of a path).
The character encoding used for the file. This defaults to the encoding specified in the file, or UTF-8. But older versions of Stata (13 and earlier) did not store the encoding used, and you'll need to specify manually. A commonly used value is "Win 1252".
Data frame to write.
Path to a file where the data will be written.
File version to use. Supports versions 8-14.
A tibble, data frame variant with nice defaults.
Variable labels are stored in the "label" attribute of each variable. It is not printed on the console, but the RStudio viewer will show it.
# NOT RUN {
path <- system.file("examples", "iris.dta", package = "haven")
read_dta(path)
tmp <- tempfile(fileext = ".dta")
write_dta(mtcars, tmp)
read_dta(tmp)
read_stata(tmp)
# }
Run the code above in your browser using DataLab