Skip to content

Instantly share code, notes, and snippets.

@multidis
Last active August 29, 2015 14:14
Show Gist options
  • Save multidis/7ac6f4779e09c986be39 to your computer and use it in GitHub Desktop.
Save multidis/7ac6f4779e09c986be39 to your computer and use it in GitHub Desktop.
Converting Julia DataFrame to R object using RCall.jl, one column at a time. String, Bool, Integer (32bit max in R), Real columns work properly. CAUTION: performance not tested for large objects.
## RCall data exchange between Julia and R
using DataFrames
using RCall
# example DataFrame
df = DataFrame(coln1 = 1:4, coln2 = ["M", "F", "F", "M"], coln3 = [false, true, false, true], coln4 = int8([1, 0, 1, 1]))
# R environment in a session started from Julia
g = globalEnv
reval(rparse("dfls <- NULL"))
# add columns one at a time converting Julia vectors to R-types via RCall.sexp
# https://github.com/JuliaStats/RCall.jl/blob/master/src/sexp.jl
for cnm in DataFrames._names(df)
g[:colcnm] = sexp(convert(Array, df[cnm]))
reval(rparse("dfls\$$cnm <- colcnm"))
end
reval(rparse("df <- data.frame(dfls)"))
reval(rparse("save(file='dfjulia.RData', df)"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment