I'm trying to do something pretty simple - just save a matrix in R and import it into julia as an array. It shouldn't be that hard, but I'm struggling to manipulate the RVector
type which is created when I read data into julia.
Here's a minimal example. In R, I run the following script:
var1 <- matrix( runif(9), 3, 3 )
save( var1, file='~/temp/file1.rda')
Then in julia:
using DataFrames, DataArrays
x = read_rda("/home/squipbar/temp/file1.rda")
I would like to assign x["var1"]
to an array variable in the julia workspace, let's call it z
. One way to do that would to be to initiate z
as an empty matrix and then fill it with the elements of x
.
The problem I'm having is that I can't access the elements of x
. As typeof
confirms, the type of x
is DataFrames.RVector{Float64,0x0e}
. This appears not to obey the standard rules of indexing in julia.
y = x["var1"] ;
y[1]
ERROR: MethodError: `getindex` has no method matching getindex(::DataFrames.RVector{Float64,0x0e}, ::Int32)
The DataFrames
documentation does not appear to offer any guidance on the RVector
type. Does anyone have any ideas where I can find that or some other explanation of how to access the elements? Even better, is there a simpler way to just pass a matrix from R to julia?
张实唯 :
is x[\"var1\"].data what you want?\n\njulia> typeof(b)\nDataFrames.RVector{Float64,0x0e}\n\njulia> b.data\n9-element Array{Float64,1}:\n 0.543234\n 0.0220151\n 0.867526\n 0.255187\n 0.710463\n 0.437579\n 0.168832\n 0.761667\n 0.600643\n\n\nor data(x[\"var1\"]) if there are NAs",
2016-06-22T04:57:14
Michael Ohlrogge :
The simplest way in my opinion would be to just save the matrix as a text file from R and then load it as a text file in Julia. Is there a reason not to do that here? For example:\n\nIn R:\n\nwrite.table(var1, file = \"/path/to/temp.txt\", sep=\"\\t\", row.names=FALSE, col.names=TRUE, quote=FALSE)\n\n\nand then in Julia:\n\nusing DataFrames\nvar1 = readtable(\"/path/to/temp.txt\", separator ='\\t');\n\n\nThis has the added advantage that it will be easy for you to view the file in a standard text editor if you want to inspect it or a piece of it, it will be easy to manipulate it with another program like python, easy to share it with other people, etc.",
2016-06-21T21:37:02