0

I'm writing a Java program which uses Rserve to get results back from R.

I need to calculate frequencies of the elements in an array. For example,

array = [A, A, B, C]

I need R to return

A  2
B  1
C  1

But when I used

Rconnection c = new Rconnection();
double [] freq = c.eval(table(x)).asDoubles;
System.out.println(Arrays.toString(freq));

I got

[2 1 1]

which is not a name:value pair. How do I get that?

Many thanks!!!!!!!!

Lin
  • 25
  • 3

1 Answers1

0

You are setting an Array of type double to the output and arrays don't have a key:value structure. Try looking at the Java data structure HashMap - that has a key:value structure.

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

If a key:value structure is not absolutely necessary, you could also attempt to use two different arrays, one which holds the "keys" and one which holds "values".

Perhaps you can set another array of type String which holds the rownames of your table(x), something like the following:

String [] key = c.eval(rownames(table(x)));
so13eit
  • 942
  • 3
  • 11
  • 22