2016年1月6日水曜日

rJava : プログラミング(3)

前回の続きで、Rの基本コマンドをrJavaで実行してみた。

1.組み合わせ数表示
[R]
[notss_analysis@ip-10-0-0-217 rjava]$ R
> choose(5,2)
[1] 10
> q()

[rJava]
$ vi JRITest03.java
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class JRITest03 {

        public static void main(String[] args)
        {
                Rengine re = new Rengine( new String[]{"--no-save"}, false, null );

                REXP x = re.eval("choose(5,2)");
                System.out.println(x);
                double [] ary = x.asDoubleArray();
                for (int i = 0; i < ary.length; i++) {
                        System.out.println(ary[i]);
                }
                re.end();
        }
}

$ javac JRITest03.java
$ java -Djava.library.path=/usr/lib64/R/library/rJava/jri/ JRITest03
[REAL* (10.0)]
10.0

2.平均算出
$ R
> mean(c(193, 204, 172, 177, 175, 155, 122, 118, 120, 139))
[1] 157.5

$ vi JRITest04.java
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class JRITest04 {

        public static void main(String[] args)
        {
                Rengine re = new Rengine( new String[]{"--no-save"}, false, null );

                REXP x = re.eval("mean(c(193, 204, 172, 177, 175, 155, 122, 118, 120, 139))");
                System.out.println(x);
                double [] ary = x.asDoubleArray();
                for (int i = 0; i < ary.length; i++) {
                        System.out.println(ary[i]);
                }
                re.end();
        }
}
$ javac JRITest04.java
$ java -Djava.library.path=/usr/lib64/R/library/rJava/jri/ JRITest04
[REAL* (157.5)]
157.5

3.summaryの表示
$ R
> summary(c(193, 204, 172, 177, 175, 155, 122, 118, 120, 139))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  118.0   126.2   163.5   157.5   176.5   204.0
> q()

$ vi JRITest05.java
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class JRITest05 {

        public static void main(String[] args)
        {
                Rengine re = new Rengine( new String[]{"--no-save"}, false, null );

                REXP x = re.eval("summary(c(193, 204, 172, 177, 175, 155, 122, 118, 120, 139))");
                System.out.println(x);
                double [] ary = x.asDoubleArray();
                for (int i = 0; i < ary.length; i++) {
                        System.out.println(ary[i]);
                }
                re.end();
        }
}
$ javac JRITest05.java
$ java -Djava.library.path=/usr/lib64/R/library/rJava/jri/ JRITest05
[REAL* (118.0, 126.2, 163.5, 157.5, 176.5, 204.0)]
118.0
126.2
163.5
157.5
176.5
204.0

0 件のコメント:

コメントを投稿