Learnning R

Install and Update R

sudo apt-get install R

If we want to update R from version A to version B, then we can

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install R

 

Install R Packages

(1)Enter into R environment                   $>R

(2)Set the working directory                   $>setwd("/media/chase/Elements/Zmy_projects/GoogleTimeSeriesAnlysis")

(3)We can also check where we are      $>getwd()

(4)Install a package named R.matlab    $>install.packages("R.matlab") //reused for reading mat file or sth else.

(5)Load the installed library                   $>library("R.matlab")

Basic Operations

(1)check and remove variables

ls() //check what variables in the workspace
rm('A')   //remove a variable named A
rm(list=ls(all=TRUE)) //remove all variables

(2)set and see where we are

getwd()
setwd('/home/chase')

(3)read from a text or csv file

firstweek<-read.table('../Data/top10080_original.txt',sep=',') // read from a text file
firstweek<-read.csv('../Data/top10080_original.txt')  //read from a csv file

Here the variable firstweek is a matrix.

(4)matrix manipulation

firstweek[1,1] //the element at first row and forst column
firstweek[,1] //the first column
firstweek[1,] //the first row
firstweek[1:1440, 1] // top 1440 elems of the first column
firstweek[1:10, 1:20] // sub matrix whose row ranges from 1 to 10, whose col ranges from 1 to 20
firstweek[,c(1,3)]// the 1st and 3rd columns

(5)matrix and vector statistics

> firstdayfirstcol<-firstweek[1:1440,1] \\ vector with 1440 elems
> length(firstweek) \\ the matrix firstweek has 33 columns
[1] 33
> sum(firstdayfirstcol)
[1] 97100.39
> length(firstdayfirstcol)
[1] 1440
> sum(firstweek)// sum all columns and rows
[1] 23232279
> colSums(firstweek) //sum each column
          V1           V2           V3           V4           V5           V6 
  688478.954   684670.075   688985.005   647922.085   643336.556   598719.240 
          V7           V8           V9          V10          V11          V12 
  438502.602   600722.034   704144.360   398789.414   577918.448     6640.911 
         V13          V14          V15          V16          V17          V18 
    4524.089     1129.896     1148.419    13561.651   313367.053   417805.373 
         V19          V20          V21          V22          V23          V24 
   95514.323    66117.268    33048.942    29394.382    33063.065    33052.713 
         V25          V26          V27          V28          V29          V30 
    2239.682    94685.033   112156.240   274774.763 15027866.000        0.000 
         V31          V32          V33 
       0.000        0.000        0.000 
> rowSums(firstweek) // sum each row
>colMeans(firstweek)
>rowMeans(firstweek)

> dim(firstmonth)// return the number of rows and cols
[1] 43200 33
> nrow(firstmonth)// return the number of rows
[1] 43200
> ncol(firstmonth)// return the number of cols
[1] 33

Java Interfaces For R

注意:一定要按步骤来

(1)sudo apt-get install build-essential  gfortran libreadline6-dev  libxt-dev 

(2)tar -zvxf R-2.13.0.tar.gz 

(3)in .bashrc.这样做是希望编译R的时候,系统能够找到jvm,以便生成java代码

export R_JAVA_LD_LIBRARY_PATH=/home/chase/runnable/jdk1.6.0_45/jre/lib/amd64/server

(4)近入解压文件,做: make build--->../configure --enable-R-shlib (这个参数必须要有)--->make:

最后会提示:


trying to compile and link a JNI progam 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
make[2]: Entering directory `/tmp/Rjavareconf.51dc6y'
gcc -std=gnu99 -I/home/chase/runnable/R-3.1.2/build/include -DNDEBUG -I/home/chase/runnable/jdk1.6.0_45/include -I/home/chase/runnable/jdk1.6.0_45/include/linux -I/usr/local/include    -fpic  -g -O2  -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/home/chase/runnable/R-3.1.2/build/lib -L/usr/local/lib64 -o conftest.so conftest.o -L/home/chase/runnable/jdk1.6.0_45/jre/lib/amd64/server -ljvm -L/home/chase/runnable/R-3.1.2/build/lib -lR
make[2]: Leaving directory `/tmp/Rjavareconf.51dc6y'


JAVA_HOME        : /home/chase/runnable/jdk1.6.0_45
Java library path: $(JAVA_HOME)/jre/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
Updating Java configuration in /home/chase/runnable/R-3.1.2/build
Done.

make[1]: Leaving directory `/home/chase/runnable/R-3.1.2/build'

然后--->make check---->make install

然后--->sudo R CMD javareconf

出现:

trying to compile and link a JNI progam 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/local/lib64/R/include -DNDEBUG -I/home/chase/runnable/jdk1.6.0_45/include -I/home/chase/runnable/jdk1.6.0_45/include/linux -I/usr/local/include    -fpic  -g -O2  -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/usr/local/lib64/R/lib -L/usr/local/lib64 -o conftest.so conftest.o -L/home/chase/runnable/jdk1.6.0_45/jre/lib/amd64/server -ljvm -L/usr/local/lib64/R/lib -lR

JAVA_HOME        : /home/chase/runnable/jdk1.6.0_45
Java library path: $(JAVA_HOME)/jre/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
Updating Java configuration in /usr/local/lib64/R
Done.

(5)根据(4)中的红色字体(这一步一定要设置

export R_HOME=/usr/local/lib64/R

(6)install rJava:

$sudo R
>install.packages(rJava)

install forecast: install.packages("forecast")

We can test if we are right:

测试文件src/test/Test1:

package test;
import org.rosuda.JRI.Rengine;


public class Test1 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Test1 t=new Test1();
                t.callRJava();

        }

           public void callRJava() {
                Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
                if (!re.waitForR()) {
                    System.out.println("Cannot load R");
                    return;
                }

                //打印变量
                String version = re.eval("R.version.string").asString();
                System.out.println(version);

                //循环打印数组
                double[] arr = re.eval("rnorm(10)").asDoubleArray();
                for (double a : arr) {
                    System.out.print(a + ",");
                }
                re.end();
            }

}

开始测试:

(1)申明动态链接库位置:libjri.so依赖与libR.so,所以需要载入$R_HOME/lib/libR.so

export LD_LIBRARY_PATH=$R_HOME/lib:/home/chase/R/x86_64-unknown-linux-gnu-library/3.1/rJava/jri/

 (2)

javac -cp /home/chase/R/x86_64-unknown-linux-gnu-library/3.1/rJava/jri/JRI.jar test/Test1.java

(3)注意红色部分。

java -cp /home/chase/R/x86_64-unknown-linux-gnu-library/3.1/rJava/jri/JRI.jar:.  test.Test1

 Data Visualization Using R

 

转载于:https://www.cnblogs.com/chaseblack/p/4174049.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值