Skip to content

Instantly share code, notes, and snippets.

@sjewo
Last active August 29, 2015 14:04
Show Gist options
  • Save sjewo/92c01155967b980803bb to your computer and use it in GitHub Desktop.
Save sjewo/92c01155967b980803bb to your computer and use it in GitHub Desktop.
Read Stata 13 files into R with rPython and pandas
# install.packages("rPython")
# requires pandas
stata13 <- function(dat,
convert.factors = TRUE){
require("rPython")
# make dat known to python
python.assign('dat',dat)
python.exec('import pandas as pd')
# convert.factors
if (convert.factors %in% c(TRUE, NA)) {
python.exec('stata = pd.read_stata(dat)')
} else{
python.exec('stata = pd.read_stata(dat, convert_categoricals=False, convert_dates=True)')
}
# panda export as json
python.exec('jso = stata.to_json()')
# # return a OrderedDict or loads will return an unordererd data.frame
python.exec('from collections import OrderedDict')
python.exec('statal = json.loads(jso, object_pairs_hook=OrderedDict)')
stata_l <- (function (var.name) {
python.command <- paste("_r_return = json.dumps( [", var.name,
"] )", sep = "")
python.exec(python.command, get.exception = FALSE)
ret <- .C("py_get_var", "_r_return", not.found.var = integer(1),
resultado = character(1), PACKAGE = "rPython")
if (ret$not.found.var)
stop("Variable not found")
#message(ret$resultado)
ret <- fromJSON(ret$resultado, nullValue=NA)
if (length(ret) == 1)
ret <- ret[[1]]
ret
})('statal')
stataDD <- data.frame(stata_l)
return(stataDD)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment