official website: http://www.r-project.org/

Taiwan

http://ftp.yzu.edu.tw/CRAN/
Department of Computer Science and Engineering, Yuan Ze University

http://cran.cs.pu.edu.tw/
Providence University, Taichung

http://cran.csie.ntu.edu.tw/
National Taiwan University, Taipei

--------------------------------------------------------------------------------------------------------------------------------

Download R 3.1.2 for Windows (54 megabytes, 32/64 bit)

image

image

image

C:\Program Files\R\R-3.1.2

image

image

image

image

image

--------------------------------------------------------------------------------------------------------------------------------------

開啟R, 好懷念的簡樸介面設計風格…

image

開始玩玩看!

> 提示符號輸入指令如下:

   1: > 6+4
   2: [1] 10
   3: > 6+4+10
   4: [1] 20
   5: > 4*2^2
   6: [1] 16
   7: > sqrt(120)
   8: [1] 10.95445
   9: > abs(-5)
  10: [1] 5
  11: > 1.2e10
  12: [1] 1.2e+10
  13: > 1.2e10/2
  14: [1] 6e+09
  15: > pi
  16: [1] 3.141593
  17: > format
  18: function (x, ...) 
  19: UseMethod("format")
  20: <bytecode: 0x0000000014014fa8>
  21: <environment: namespace:base>
  22: > 

測試matrix指令, 發現MATLAB語法還是比較直覺…

   1: > 1:6
   2: [1] 1 2 3 4 5 6
   3: > matrix(1:12, nrow =4, ncol=3)
   4:      [,1] [,2] [,3]
   5: [1,]    1    5    9
   6: [2,]    2    6   10
   7: [3,]    3    7   11
   8: [4,]    4    8   12
   9: > matrix(1:12/10, nrow =4, ncol=3)
  10:      [,1] [,2] [,3]
  11: [1,]  0.1  0.5  0.9
  12: [2,]  0.2  0.6  1.0
  13: [3,]  0.3  0.7  1.1

14: [4,] 0.4 0.8 1.2

變數指派:   名稱= 物件or 名稱←物件

   1: > n1<--3
   2: > n1
   3: [1] -3
   4: > n2 = -3

矩陣運算

   1: > v1=matrix(1:12/10, nrow =4, ncol=3)
   2: > v1
   3:      [,1] [,2] [,3]
   4: [1,]  0.1  0.5  0.9
   5: [2,]  0.2  0.6  1.0
   6: [3,]  0.3  0.7  1.1
   7: [4,]  0.4  0.8  1.2
   8: > 10*v1
   9:      [,1] [,2] [,3]
  10: [1,]    1    5    9
  11: [2,]    2    6   10
  12: [3,]    3    7   11
  13: [4,]    4    8   12
  14: > 1+v1
  15:      [,1] [,2] [,3]
  16: [1,]  1.1  1.5  1.9
  17: [2,]  1.2  1.6  2.0
  18: [3,]  1.3  1.7  2.1
  19: [4,]  1.4  1.8  2.2

取出1,2列, 1~3行的資料

   1: > v1[1:2,1:3]
   2:      [,1] [,2] [,3]
   3: [1,]  0.1  0.5  0.9
   4: [2,]  0.2  0.6  1.0

矩陣布林運算

   1: > v1
   2:      [,1] [,2] [,3]
   3: [1,]  0.1  0.5  0.9
   4: [2,]  0.2  0.6  1.0
   5: [3,]  0.3  0.7  1.1
   6: [4,]  0.4  0.8  1.2
   7: > v1>0.5
   8:       [,1]  [,2] [,3]
   9: [1,] FALSE FALSE TRUE
  10: [2,] FALSE  TRUE TRUE
  11: [3,] FALSE  TRUE TRUE
  12: [4,] FALSE  TRUE TRUE

矩陣>0.5的數值取出

   1: > ind = v1>0.5
   2: > v1
   3:      [,1] [,2] [,3]
   4: [1,]  0.1  0.5  0.9
   5: [2,]  0.2  0.6  1.0
   6: [3,]  0.3  0.7  1.1
   7: [4,]  0.4  0.8  1.2
   8: > v1[ind]
   9: [1] 0.6 0.7 0.8 0.9 1.0 1.1 1.2

第1行第1~3列取平均

   1: > mean(v1[1:3,1])
   2: [1] 0.2

var(v1)

   1: > var(v1)
   2:            [,1]       [,2]       [,3]
   3: [1,] 0.01666667 0.01666667 0.01666667
   4: [2,] 0.01666667 0.01666667 0.01666667
   5: [3,] 0.01666667 0.01666667 0.01666667

輸入3x2的陣矩

   1: > B = matrix( c(1, 2, 3, 1.1, 1.5, 1.2), nrow=3, ncol=2)
   2: > B
   3:      [,1] [,2]
   4: [1,]    1  1.1
   5: [2,]    2  1.5
   6: [3,]    3  1.2

繪圖囉!!!

> plot(B[1:3, 1], B[1:3, 2])

image

初始化table, 內容為1:18也是同MATLAB column wise

   1: > table<-as.data.frame(matrix(1:18, ncol=6))
   2: > table
   3:   V1 V2 V3 V4 V5 V6
   4: 1  1  4  7 10 13 16
   5: 2  2  5  8 11 14 17
   6: 3  3  6  9 12 15 18

取出第1行全部

   1: > table[,1]
   2: [1] 1 2 3

取出第2行, V2的第一個字大寫!

   1: > table[,1]
   2: [1] 1 2 3
   3: > table$v2
   4: NULL
   5: > table$V2
   6: [1] 4 5 6

取出部分矩陣

   1: > table[2:3, 1:2]
   2:   V1 V2
   3: 2  2  5
   4: 3  3  6

table矩陣轉置(transpose)

   1: > t(table)
   2:    [,1] [,2] [,3]
   3: V1    1    2    3
   4: V2    4    5    6
   5: V3    7    8    9
   6: V4   10   11   12
   7: V5   13   14   15
   8: V6   16   17   18

列出目前workspace所有變數

   1: > ls()
   2: [1] "B"     "ind"   "n1"    "n2"    "table" "v1"    "x"     "x1"   
   3: > 

參考來源:

1. http://www.r-tutor.com/r-introduction/matrix/matrix-construction 

2. http://eln.iis.sinica.edu.tw/lims/files/users/admin/rde_packagean_zhuang_ji_ji_ben_cao_zuo_.pdf

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()