我可以使用JRI使用Java的R环境,但我想知道是否有更好的方法来创建数据帧而不是以下(使用数组的Scala语法):
r.assign("predictor1", Array(1,2,3,1)) r.assign("predictor2", Array("a","b","a","c")) r.assign("class", Array("y","n","y","n")) r.eval("df = data.frame(predictor1=predictor1, predictor2=predictor2, class=class)")除了有点麻烦之外,请注意我是如何污染全局命名空间并意外地破坏了标准class函数。
为了解决后两个问题,我还尝试首先创建一个空的data.frame,然后调用r.assign("df$predictor1", Array(1,2,3,1)) ,但这不起作用 -它分配给名为df$predictor 。
I can use an R environment from Java using JRI, but I'm wondering if there's a better way to create data frames than the following (using Scala syntax for arrays):
r.assign("predictor1", Array(1,2,3,1)) r.assign("predictor2", Array("a","b","a","c")) r.assign("class", Array("y","n","y","n")) r.eval("df = data.frame(predictor1=predictor1, predictor2=predictor2, class=class)")Besides being a bit cumbersome, note how I've just polluted the global namespace and accidentally clobbered the standard class function.
Attempting to remedy the latter two problems, I also tried first creating an empty data.frame and then calling r.assign("df$predictor1", Array(1,2,3,1)), but that doesn't work - it assigns to a variable named df$predictor.
最满意答案
事实证明JRI有两个抽象层次(JRI和REngine),而我正在寻找错误的抽象层次(JRI)。 REngine中的REXP有一个createDataFrame()方法:
http://rforge.net/org/doc/org/rosuda/REngine/REXP.html
Turns out JRI has two levels of abstraction (JRI and REngine) and I was looking at the wrong one (JRI). REXP in REngine has a createDataFrame() method:
http://rforge.net/org/doc/org/rosuda/REngine/REXP.html
更多推荐
发布评论