1. Matrix Algebra
R for MATLAB users: http://mathesaurus.sourceforge.net/octave-r.html(区分R和MATLAB的指令/外网)
> set.seed(123) #100个数里随机取15个,5行3列
> A = matrix(sample(100,15), nrow=5, ncol=3)
> set.seed(234)
> B = matrix(sample(100,15), nrow=5, ncol=3)
> set.seed(321)
> X = matrix(sample(100,25), nrow=5, ncol=5)
> set.seed(213)
> b = matrix(sample(100,5),nrow=5, ncol=1)
> A
[,1] [,2] [,3]
[1,] 31 42 90
[2,] 79 50 69
[3,] 51 43 57
[4,] 14 97 9
[5,] 67 25 72
> B
[,1] [,2] [,3]
[1,] 97 18 55
[2,] 31 56 54
[3,] 34 1 63
[4,] 46 68 79
[5,] 98 92 43
> X
[,1] [,2] [,3] [,4] [,5]
[1,] 54 17 82 36 51
[2,] 77 47 79 78 13
[3,] 88 11 75 34 30
[4,] 80 25 91 4 89
[5,] 58 31 90 48 81
> b
[,1]
[1,] 56
[2,] 44
[3,] 16
[4,] 73
[5,] 61
# + - * / ^
#Element-wise addition, subtraction, multiplication, division
#, and exponentiation, respectively.
> A + 2
[,1] [,2] [,3]
[1,] 33 44 92
[2,] 81 52 71
[3,] 53 45 59
[4,] 16 99 11
[5,] 69 27 74
> A * 2
[,1] [,2] [,3]
[1,] 62 84 180
[2,] 158 100 138
[3,] 102 86 114
[4,] 28 194 18
[5,] 134 50 144
> A ^ 2
[,1] [,2] [,3]
[1,] 961 1764 8100
[2,] 6241 2500 4761
[3,] 2601 1849 3249
[4,] 196 9409 81
[5,] 4489 625 5184
#Matrix multiplication
> t(A) %*% B # t(A): transports(A的转置矩阵)
[,1] [,2] [,3]
[1,] 14400 12149 13171
[2,] 13998 12495 16457
[3,] 20277 12777 16074
#Returns a vector containing the column means of A.
> colMeans(A)
[1] 48.4 51.4 59.4
#Returns a vector containing the column sums of A.
> colSums(A)
[1] 242 257 297
> #Returns a vector containing the row means of A.
> rowMeans(A) #每一行的平均数
[1] 54.33333 66.00000 50.33333 40.00000
[5] 54.66667
> #Returns a vector containing the row sums of A.
> rowSums(A) #每一行的和
[1] 163 198 151 120 164
> #Matrix Crossproduct
> # A'A # A×t(A)
> crossprod(A) # 矩阵的叉乘
[,1] [,2] [,3]
[1,] 14488 10478 16098
[2,] 10478 16147 12354
[3,] 16098 12354 21375
> # A'B
> crossprod(A,B)
[,1] [,2] [,3]
[1,] 14400 12149 13171
[2,] 13998 12495 16457
[3,] 20277 12777 16074
> #Inverse of A where A is a square matrix.
> solve(X) #
[,1] [,2] [,3] [,4] [,5]
[1,] -0.038716716 -0.001615536 0.02639602 0.001865776 0.01281012
[2,] -0.007833594 0.029329714 -0.03505452 0.029705074 -0.01943073
[3,] 0.070634812 0.005604153 -0.02616163 0.007748418 -0.04419740
[4,] -0.022901322 -0.006899276 0.01956364 -0.026670636 0.03758562
[5,] -0.034190849 -0.012206525 0.01199028 -0.005509128 0.03744472
> #Solves for vector x in the equation b = Ax.
> # b = Xv
> v = solve(X, b)
> v
[,1]
[1,] -0.8992643
[2,] 1.2741498
[3,] 1.6531390
[4,] -0.9272574
[5,] -0.3779688
> #Returns a vector containing the elements of the principal diagonal
> diag(X) # 返回矩阵的对角线
[1] 54 47 75 4 81
> X
[,1] [,2] [,3] [,4] [,5]
[1,] 54 17 82 36 51
[2,] 77 47 79 78 13
[3,] 88 11 75 34 30
[4,] 80 25 91 4 89
[5,] 58 31 90 48 81
> #Creates a diagonal matrix with the elements of x in the principal diagonal.
> diag(c(1,2,3,4)) # 创建对角线矩阵
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 2 0 0
[3,] 0 0 3 0
[4,] 0 0 0 4
> #If k is a scalar, this creates a k x k identity matrix.
> diag(5) # diag(x): x行,x列的单位矩阵
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 1 0 0 0
[3,] 0 0 1 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
> #Eigenvalues and eigenvectors of A.
> eigen(X) #本征函数
eigen() decomposition
$values
[1] 264.12614+ 0.00000i
[2] -39.97988+ 0.00000i
[3] 26.02694+12.60671i
[4] 26.02694-12.60671i
[5] -15.20015+ 0.00000i
$vectors
[,1] [,2]
[1,] 0.3893077+0i 0.08721116+0i
[2,] 0.4742054+0i 0.55199369+0i
[3,] 0.3745159+0i 0.10652480+0i
[4,] 0.4712054+0i -0.82000026+0i
[5,] 0.5111477+0i 0.06284292+0i
[,3]
[1,] -0.05950991+0.03631631i
[2,] 0.72745041+0.00000000i
[3,] -0.03767373+0.38696839i
[4,] -0.08196570-0.22750249i
[5,] -0.10038826-0.49622393i
[,4]
[1,] -0.05950991-0.03631631i
[2,] 0.72745041+0.00000000i
[3,] -0.03767373-0.38696839i
[4,] -0.08196570+0.22750249i
[5,] -0.10038826+0.49622393i
[,5]
[1,] -0.5138367+0i
[2,] 0.2763409+0i
[3,] 0.6846989+0i
[4,] -0.3671550+0i
[5,] -0.2366265+0i
# or
> eigen(X)
eigen() decomposition
$values
[1] 264.12614+ 0.00000i -39.97988+ 0.00000i 26.02694+12.60671i 26.02694-12.60671i -15.20015+ 0.00000i
$vectors
[,1] [,2] [,3] [,4] [,5]
[1,] 0.3893077+0i 0.08721116+0i -0.05950991+0.03631631i -0.05950991-0.03631631i -0.5138367+0i
[2,] 0.4742054+0i 0.55199369+0i 0.72745041+0.00000000i 0.72745041+0.00000000i 0.2763409+0i
[3,] 0.3745159+0i 0.10652480+0i -0.03767373+0.38696839i -0.03767373-0.38696839i 0.6846989+0i
[4,] 0.4712054+0i -0.82000026+0i -0.08196570-0.22750249i -0.08196570+0.22750249i -0.3671550+0i
[5,] 0.5111477+0i 0.06284292+0i -0.10038826-0.49622393i -0.10038826+0.49622393i -0.2366265+0i
2. Afterword
Google & English
The R Project (http://www.r-project.org/): The official R website and your first stop for all things R. The site includes extensive documentation, including An Introduction to R, The R Language Definition, Writing R Extensions, R Data Import/Export, R Installation and Administration, and The R FAQ.
(官方R网站)
The R Journal (http://journal.r-project.org/): A freely accessible refereed journal containing articles on the R project and contributed packages.
(R免费期刊)
R Bloggers (http://www.r-bloggers.com/): A central hub (blog aggregator) collecting content from bloggers writing about R. Contains new articles daily. I am addicted to it.
(R最新资讯,教程)
Planet R (http://planetr.stderr.org): Another good site-aggregator, includ�ing information from a wide range of sources. Updated daily.
R Graph Gallery (http://addictedtor.free.fr/graphiques/): A collection of
innovative graphs, along with their source code.
R Graphics Manual (http://bm2.genes.nig.ac.jp/): A collection of R graphics from all R packages, arranged by topic, package, and function. At last count, there were 35,000+ images!
(R作图技巧,提供源代码,包)
Journal of Statistical Software (http://www.jstatsoft.org/): A freely ac�cessible refereed journal containing articles, book reviews, and code snippets on statistical computing. Contains frequent articles about R.
(学界统计学)
Quick-R (http://www.statmethods.net): The website of R in Action author.
(R author)