除此之外還可以用append函數(shù)為原有的向量添加元素 > append(t,10:15) [1] 2 543 3 5 1 10 11 12 13 14 15 Append 追加元素: 追加之后原來的向量并沒有改變其元素取值;缺省關(guān)鍵字after時(shí)表示從向量的末尾追加元素 > c <- c(1,2,3,4) > prod(c) [1] 24 > append(c,6:8) [1] 1 2 3 4 6 7 8 > append(c,9:12,after=3) [1] 1 2 3 9 10 11 12 4 > prod(c) [1] 24
向量下標(biāo)運(yùn)算: R語言里向量下標(biāo)從1開始; 例1: 創(chuàng)建一個(gè)向量,讓其每個(gè)元素加4,求其第二個(gè)元素是幾? > (c(1,3,5,7)+4)[2] [1] 7 例2: 元素替換 > t [1] 2 543 3 5 1 > t[2] <- 10> T [1] 2 10 3 5 1 例3
> t [1] 2 543 3 5 1 > t[c(1,3)] <- c(9,11) > t [1] 9 10 11 5 1
向量的邏輯運(yùn)算: > t [1] 9 10 11 5 1 > t[t<4] #顯示出t<4的數(shù) [1] 1 > t[t>9]#顯示出t>9的數(shù) [1] 10 11
把向量中所有缺省值賦值為0 新建一個(gè)向量,其中包括缺省值 > x<-c(-1,2,5,6,NA) > x [1] -1 2 5 6 NA > x[is.na(x)]<-0 > x [1] -1 2 5 6 0
把向量中不是NA的值導(dǎo)入到另一個(gè)向量中去 > x<-c(-1,2,5,6,NA) > x [1] -1 2 5 6 NA > u<-x[!is.na(x)] > u [1] -1 2 5 6
將變量s設(shè)為數(shù)值型,長(zhǎng)度為向量X的長(zhǎng)度,則s的默認(rèn)元素都是0 > x [1] -1 2 5 6 NA > s<-numeric(length(x)) > s [1] 0 0 0 0 0
分段函數(shù)的表示方法: > y [1] 1 3 5 7 9 > s<-numeric(length(y)) > s [1] 0 0 0 0 0 > s[s<0] <- y[y<0] > s[s>=0] <- y[y>=0] +1 > s [1] 2 4 6 8 10
如果x是一個(gè)有缺失值的向量、則在對(duì)s賦值的時(shí)候會(huì)報(bào)錯(cuò) > s<-numeric(length(x)) > s [1] 0 0 0 0 0 > s[s<0] <- x[x<0] > s[s>=0] <- x[x>=0] +1 Warning message:In s[s >= 0] <- x[x >= 0] + 1 : number of items to replace is not a multiple of replacement length
向量1-5的元素不顯示: > x <- c(1:10) > x [1] 1 2 3 4 5 6 7 8 9 10 > x[-(1:5)] [1] 6 7 8 9 10
R語言的矩陣 矩陣就是用數(shù)據(jù)排列成長(zhǎng)和寬的表二維數(shù)組,單元必須是相同的數(shù)據(jù)類型;用列表示不同的變量,用行表示各個(gè)對(duì)象;R語言生成矩陣函數(shù)--matrix 語法: Mymatrix <- matrix(vector,nrow=number_of_rows,ncol=number_of_columns, Byrow=logical_value,dimnames=list( Char_vector_rowames,char_vector_colnames))
第一個(gè)參數(shù)是數(shù)據(jù);第二個(gè)參數(shù)是行數(shù);第三個(gè)參數(shù)是列數(shù);第四個(gè)參數(shù)表示:安行展開或者安列展開;第五個(gè)參數(shù)表示矩陣的維數(shù);
> A <- matrix(1:12,nrow=3,ncol=4) > A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > T(A) Error: could not find function "T" > t(A) #矩陣的轉(zhuǎn)秩 [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > t(x) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 2 3 4 5 6 7 8 9 10
矩陣的簡(jiǎn)單運(yùn)算 > A <- matrix(1:12,nrow=3,ncol=4) > A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > t(A) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 > B<-A > A+B [,1] [,2] [,3] [,4] [1,] 2 8 14 20 [2,] 4 10 16 22 [3,] 6 12 18 24 > A-B [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 > 3*A [,1] [,2] [,3] [,4] [1,] 3 12 21 30 [2,] 6 15 24 33 [3,] 9 18 27 36 > B <- t(A) > #兩個(gè)矩陣相乘 > A%*%B [,1] [,2] [,3] [1,] 166 188 210 [2,] 188 214 240 [3,] 210 240 270
求矩陣的對(duì)角線元素: > A <- matrix(1:16,nrow=4) > A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > diag(A) [1] 1 6 11 16 > diag(diag(A)) [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16 > diag(4) #生成對(duì)角元素值為1的單位矩陣 [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 1 0 0 [3,] 0 0 1 0 [4,] 0 0 0 1
矩陣的求逆運(yùn)算:solve()函數(shù);
> A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > solve(A) #A不是可逆的矩陣 Error in solve.default(A) : Lapack routine dgesv: system is exactly singular: U[3,3] = 0
#隨機(jī)生成一個(gè)遵循正太分布的矩陣,避免它出現(xiàn)不可逆的情況,4X4的矩陣 A <- matrix(rnorm(16),4,4) solve(A) > A <- matrix(rnorm(16),4,4) > A [,1] [,2] [,3] [,4] [1,] -0.70756823 -1.1234569 1.360924 -0.2375963 [2,] 1.97157201 -1.3441301 1.753795 -1.2241501 [3,] -0.08999868 -1.5231558 1.568365 -0.3278127 [4,] -0.01401725 -0.4219682 1.296756 -2.4124503 > solve(A) [,1] [,2] [,3] [,4] [1,] -0.05180593 0.4723860 -0.3259291 -0.1903122 [2,] 4.46734372 1.3701835 -5.0357807 -0.4509696 [3,] 4.70043705 1.4725733 -4.6045433 -0.5844802 [4,] 1.74551456 0.5491397 -1.5923475 -0.6487037
> solve(A)%*%A #矩陣A和矩陣A的逆做運(yùn)算應(yīng)該是一個(gè)單位陣,由于計(jì)算機(jī)的計(jì)算誤差出現(xiàn)了非對(duì)角元素趨向于0的誤差
[,1] [,2] [,3] [,4] [1,] 1.000000e+00 -1.151856e-15 1.137979e-15 -2.220446e-16 [2,] -5.238865e-16 1.000000e+00 1.665335e-15 -4.440892e-16 [3,] 2.255141e-17 -2.053913e-15 1.000000e+00 -2.220446e-16 [4,] -1.387779e-17 -2.220446e-16 -3.330669e-16 1.000000e+00
求特征值和特征向量:eigen()函數(shù) > A [,1] [,2] [,3] [,4] [1,] 2 1 1 1 [2,] 1 2 1 1 [3,] 1 1 2 1 [4,] 1 1 1 2 > (A.eigen <- eigen(A,symmetric = T)) $values #特征值就是:5,1,1,1 [1] 5 1 1 1
$vectors #特征向量 [,1] [,2] [,3] [,4] [1,] -0.5 0.8660254 0.0000000 0.0000000 [2,] -0.5 -0.2886751 -0.5773503 -0.5773503 [3,] -0.5 -0.2886751 -0.2113249 0.7886751 [4,] -0.5 -0.2886751 0.7886751 -0.2113249
將矩陣變換為上三角矩陣:chol()函數(shù) > A [,1] [,2] [,3] [,4] [1,] 2 1 1 1 [2,] 1 2 1 1 [3,] 1 1 2 1 [4,] 1 1 1 2 > chol(A) [,1] [,2] [,3] [,4] [1,] 1.414214 0.7071068 0.7071068 0.7071068 [2,] 0.000000 1.2247449 0.4082483 0.4082483 [3,] 0.000000 0.0000000 1.1547005 0.2886751 [4,] 0.000000 0.0000000 0.0000000 1.1180340 > t(chol(A))%*%chol(A) [,1] [,2] [,3] [,4] [1,] 2 1 1 1 [2,] 1 2 1 1 [3,] 1 1 2 1 [4,] 1 1 1 2
如果矩陣式對(duì)稱的,就可以用chol()來求行列式的值以及舉證的逆
dim(A)對(duì)矩陣A的維數(shù); nrow(A)矩陣A的行數(shù); ncol(A)矩陣A的列數(shù); rowSums(A)對(duì)行求和; colSums(A)對(duì)列求和 colMeans()對(duì)矩陣的行和列求均值
> A [,1] [,2] [,3] [,4] [1,] 2 1 1 1 [2,] 1 2 1 1 [3,] 1 1 2 1 [4,] 1 1 1 2 > dim(A) [1] 4 4 > nrow(A) [1] 4 > ncol(A) [1] 4 > rowSums(A) [1] 5 5 5 5 > colSums(A) [1] 5 5 5 5
方法一:矩陣的上三角矩陣和下三角矩陣:up.tri();low.tri(); > A<- matrix(1:16,4) > A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > lower.tri(A) [,1] [,2] [,3] [,4] [1,] FALSE FALSE FALSE FALSE [2,] TRUE FALSE FALSE FALSE [3,] TRUE TRUE FALSE FALSE [4,] TRUE TRUE TRUE FALSE > A[lower.tri(A)] <-0 #下三角矩陣 > A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 0 6 10 14 [3,] 0 0 11 15 [4,] 0 0 0 16 > A[up.tri(A)] <- 0 Error in A[up.tri(A)] <- 0 : could not find function "up.tri" > A[upper.tri(A)] <- 0 #上三角矩陣 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16 > A<- matrix(1:16,4) > A[upper.tri(A)] <- 0 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 6 0 0 [3,] 3 7 11 0 [4,] 4 8 12 16 > A[lower.tri(A)] =0 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16
方法二:求矩陣的下三角陣和上三角陣 讓行下標(biāo)的位置小于列下表的位置的元素都為0,----下三角陣;
> A<- matrix(1:16,4) > A [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > row(A) [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 [3,] 3 3 3 3 [4,] 4 4 4 4 > col(A) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 1 2 3 4 [3,] 1 2 3 4 [4,] 1 2 3 4 > A[row(A)<col(A)]=0 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 2 6 0 0 [3,] 3 7 11 0 [4,] 4 8 12 16 > A[row(A)>col(A)]=0 > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16
矩陣中計(jì)算行列式的值:det(A) > A [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 6 0 0 [3,] 0 0 11 0 [4,] 0 0 0 16 > det(A) [1] 1056
將矩陣轉(zhuǎn)化為向量化算子(這里需要編寫一個(gè)小函數(shù)) > vec<-function(x){ + t(t(as.vector(x))) + } > A<-matrix(1:12,3,4) > vec(A) [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 [11,] 11 [12,] 12 根據(jù)矩陣的下標(biāo)去取元素 > A [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > A(2,3) Error: could not find function "A" > A[2,3] [1] 8 > A[,3] [1] 7 8 9 > A[2,] [1] 2 5 8 11 > A[1:3,2] #取出1-3行的第二列元素 [1] 4 5 6 |
|