array( 初始值, 維度)
產生2x3陣列裡面元素為0
1: > x<-array(0, dim=c(2,3)) 2: > x 3: [,1] [,2] [,3] 4: [1,] 0 0 0 5: [2,] 0 0 0產生2x3陣列裡面元素為-1
1: > x<-array(-1, dim=c(2,3)) 2: > x 3: [,1] [,2] [,3] 4: [1,] -1 -1 -1 5: [2,] -1 -1 -1假設故意初始化一個長度超過指定陣列元素總數量, 聰明的R也可以自動判定, 儲存到總個數上限
1: > x<-array(1:7, dim=c(2,3)) 2: > x 3: [,1] [,2] [,3] 4: [1,] 1 3 5 5: [2,] 2 4 6將x進行轉置並存回
1: > x=t(x) 2: > x 3: [,1] [,2] 4: [1,] 1 2 5: [2,] 3 4 6: [3,] 5 6產生索引, 其中column 1為列的索引[1 2 3]’;而column 2 為行的索引[2 1 1]’
1: > ind<-array(c(1:3, 2:1), dim=c(3,2)) 2: > ind 3: [,1] [,2] 4: [1,] 1 2 5: [2,] 2 1 6: [3,] 3 1所以取出對應ind的x數值, 也就是分別取出x(1,2), x(2,1) 和x(3,1)的意思
1: > x[ind] 2: [1] 2 3 5同樣的, 若要將對應ind的x數值取代成-100
1: > x[ind] = -100 2: > x 3: [,1] [,2] 4: [1,] 1 -100 5: [2,] -100 4 6: [3,] -100 6array: array(data_vector, dim_vector)
產生2x2x3的向量
1: > x <- array(1:12, dim=c(2,2,3)) 2: > x 3: , , 1 4: 5: [,1] [,2] 6: [1,] 1 3 7: [2,] 2 4 8: 9: , , 2 10: 11: [,1] [,2] 12: [1,] 5 7 13: [2,] 6 8 14: 15: , , 3 16: 17: [,1] [,2] 18: [1,] 9 11 19: [2,] 10 12matrix
1: > x <-matrix(0, nrow=3, ncol=4) 2: > x 3: [,1] [,2] [,3] [,4] 4: [1,] 0 0 0 0 5: [2,] 0 0 0 0 6: [3,] 0 0 0 0另一種方式
1: > n=3;b=2 2: > Xb <- matrix(0, n, b) 3: > Xb 4: [,1] [,2] 5: [1,] 0 0 6: [2,] 0 0 7: [3,] 0 0Outer Product of Arrays
1: > d <- outer(0:9, 0:9) 2: > d 3: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] 4: [1,] 0 0 0 0 0 0 0 0 0 0 5: [2,] 0 1 2 3 4 5 6 7 8 9 6: [3,] 0 2 4 6 8 10 12 14 16 18 7: [4,] 0 3 6 9 12 15 18 21 24 27 8: [5,] 0 4 8 12 16 20 24 28 32 36 9: [6,] 0 5 10 15 20 25 30 35 40 45 10: [7,] 0 6 12 18 24 30 36 42 48 54 11: [8,] 0 7 14 21 28 35 42 49 56 63 12: [9,] 0 8 16 24 32 40 48 56 64 72 13: [10,] 0 9 18 27 36 45 54 63 72 81產生2x2x3陣列
1: > x <-array(1:12, c(2,2,3)) 2: > x 3: , , 1 4: 5: [,1] [,2] 6: [1,] 1 3 7: [2,] 2 4 8: 9: , , 2 10: 11: [,1] [,2] 12: [1,] 5 7 13: [2,] 6 8 14: 15: , , 3 16: 17: [,1] [,2] 18: [1,] 9 1119: [2,] 10 12
超過2維陣列轉置不可以使用t()
1: > t(x)2: 錯誤在t.default(x) : 引數不是矩陣
要用令用aperm()
1: > x 2: , , 1 3: 4: [,1] [,2] 5: [1,] 1 3 6: [2,] 2 4 7: 8: , , 2 9: 10: [,1] [,2] 11: [1,] 5 7 12: [2,] 6 8 13: 14: , , 3 15: 16: [,1] [,2] 17: [1,] 9 11 18: [2,] 10 12 19: 20: > aperm(x) 21: , , 1 22: 23: [,1] [,2] 24: [1,] 1 3 25: [2,] 5 7 26: [3,] 9 11 27: 28: , , 2 29: 30: [,1] [,2] 31: [1,] 2 4 32: [2,] 6 8 33: [3,] 10 12 34: 矩陣 A*B
語法: A %*% B
1: > A 2: [,1] [,2] 3: [1,] 1 3 4: [2,] 2 4 5: > B<-array(c(1,2,3,4), dim=c(2,2)) 6: > B 7: [,1] [,2] 8: [1,] 1 3 9: [2,] 2 4 10: > A %*% B 11: [,1] [,2] 12: [1,] 7 15 13: [2,] 10 22A .* B 對應元素位置相乘
語法: A*B
1: > A*B 2: [,1] [,2] 3: [1,] 1 9 4: [2,] 4 16The inverse of A
語法: solve( A )
1: > solve(A) 2: [,1] [,2] 3: [1,] -2 1.5 4: [2,] 1 -0.5假設 Ax = b, 則 x = inv( A )*b
語法 x = solve(A, b)
1: > b = array(1:2, dim=c(2,1)) 2: > b 3: [,1] 4: [1,] 1 5: [2,] 2 6: > solve(A, b) 7: [,1] 8: [1,] 1 9: [2,] 0List 是一種有順序性收納(an ordered collection)資料型態, 可以收集許多物件也就是它的組成元素
1: > Lst <- list(name="Fred", wife="Mary", no.children=3,
2: + child.ages=c(4,7,9)) 3: > Lst 4: $name5: [1] "Fred"
6: 7: $wife8: [1] "Mary"
9: 10: $no.children 11: [1] 3 12: 13: $child.ages 14: [1] 4 7 9甚麼是有順序性?由下列範例可以知道:
1: > Lst[1] 2: $name3: [1] "Fred"
4: 5: > Lst[2] 6: $wife7: [1] "Mary"
8: 9: > Lst[3] 10: $no.children 11: [1] 3 12: 13: > Lst[4] 14: $child.ages 15: [1] 4 7 9Lst筆數
1: > length(Lst) 2: [1] 4取出Lst對應不同欄位的數值, 語法: 變數名稱.$欄位名稱
1: > Lst$name2: [1] "Fred"
3: > Lst$wife4: [1] "Mary"
5: > Lst$child.ages[0] 6: numeric(0) 7: > Lst$child.ages[1] 8: [1] 4 9: > Lst$child.ages[2] 10: [1] 7 11: > Lst$child.ages[3] 12: [1] 9另外, 也可以 變數名稱[“欄位名稱”]
1: > Lst["name"]
2: $name3: [1] "Fred"
4: 5: > Lst["wife"]
6: $wife7: [1] "Mary"
可是卻沒有辦法取出
1: > Lst["child"]
2: $<NA> 3: NULL 4: 反而要像這樣
1: > Lst["child.ages"]
2: $child.ages 3: [1] 4 7 9也可以用剛才索引的方式取出
1: > Lst[4][1] 2: $child.ages 3: [1] 4 7 9若要個別物件取出則需要兩層[]
1: > Lst[[1]]2: [1] "Fred"
3: > Lst[[2]]4: [1] "Mary"
5: > Lst[[3]] 6: [1] 3 7: > Lst[[4]] 8: [1] 4 7 9其中第4個物件有三個元素
1: > Lst[[4]][1] 2: [1] 4 3: > Lst[[4]][2] 4: [1] 7 5: > Lst[[4]][3] 6: [1] 9文章標籤
全站熱搜
