Convert rexp objects to double arrays (Java / R)
I want r to generate normal data and then use it in Java I know a function that converts rexp objects into arrays, but it doesn't seem to work This is what I have:
REXP x; x = re.eval("rnorm(100,50,10)"); double[] test = x.asDoubleArray(); System.out.println(x); System.out.println(test);
I'll print it out to see what's wrong The results are as follows:
[REAL* (61.739814266023316,40.25177570831545,36.09450830843867,48.06821029847672,...etc)] [D@61de33
The problem is how r returns the result to Java; It tells Java what x is. If they are strings, it will say [string * (.. whatever...)] I just want what's in parentheses The line it returns is also a string
I will use big data, so I hope it is fast I have tried to use subsets, extract the contents in parentheses, and then parse them into double precision, but there must be a better solution In addition, this does not seem to apply to data over 100 points
Solution
Now that we have mentioned this question, let's answer:
System out. Println (test); Test is double [], which literally means system out. println(test.toString());
The problem is that in Java, the toString () implementation of arrays is poor Therefore, in order to get the results you need, you need to use
REXP x; x = re.eval("rnorm(100,10)"); double[] test = x.asDoubleArray(); System.out.println(x); System.out.println(Arrays.asList(test)); // note we get an array-backed list here
Because the list has the appropriate toString () method
Once again, I apologize for an obvious answer