R for MATLAB users:
http://mathesaurus.sourceforge.net/octave-r.html
R语言和MATLAB功能比较
矩阵运算
导入数据
> set.seed(123)
> A = matrix(sample(100,15), nrow=5, ncol=3)#sample(100,15)100个数随机取15个
> 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
+ - * / ^ 运算
对应矩阵内每一个数都进行运算
其中a^x为a的x次幂
> A
[,1] [,2] [,3]
[1,] 31 42 90
[2,] 79 50 69
[3,] 51 43 57
[4,] 14 97 9
[5,] 67 25 72
> 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#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
矩阵乘法
> t(A)#矩阵的转置
[,1] [,2] [,3] [,4] [,5]
[1,] 31 79 51 14 67
[2,] 42 50 43 97 25
[3,] 90 69 57 9 72
> t(A) %*% B #(3,5)*(5,3)=(3,3)
[,1] [,2] [,3]
[1,] 14400 12149 13171
[2,] 13998 12495 16457
[3,] 20277 12777 16074
矩阵的一些函数
colMeans()
矩阵每列的平均值colSums()
矩阵每列的和rowMeans()
矩阵每行的平均值rowSums()
矩阵每行的和
> A
[,1] [,2] [,3]
[1,] 31 42 90
[2,] 79 50 69
[3,] 51 43 57
[4,] 14 97 9
[5,] 67 25 72
> colMeans(A)
[1] 48.4 51.4 59.4
> colSums(A)
[1] 242 257 297
> rowMeans(A)
[1] 54.33333 66.00000 50.33333 40.00000
[5] 54.66667
> rowSums(A)
[1] 163 198 151 120 164
crossprod()
矩阵的叉乘
#A*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
solve()求矩阵的逆或者解线性方程
> 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
> solve(X)#求X的逆
[,1] [,2] [,3]
[1,] -0.038716716 -0.001615536 0.02639602
[2,] -0.007833594 0.029329714 -0.03505452
[3,] 0.070634812 0.005604153 -0.02616163
[4,] -0.022901322 -0.006899276 0.01956364
[5,] -0.034190849 -0.012206525 0.01199028
[,4] [,5]
[1,] 0.001865776 0.01281012
[2,] 0.029705074 -0.01943073
[3,] 0.007748418 -0.04419740
[4,] -0.026670636 0.03758562
[5,] -0.005509128 0.03744472
> b
[,1]
[1,] 56
[2,] 44
[3,] 16
[4,] 73
[5,] 61
> v=solve(X,b)#解b=Xv
> v
[,1]
[1,] -0.8992643
[2,] 1.2741498
[3,] 1.6531390
[4,] -0.9272574
[5,] -0.3779688
diag(X)
返回矩阵X的对角线值
diag()
还可以用于创建矩阵
> 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
> diag(X)
[1] 54 47 75 4 81
> 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
> diag(5)
[,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
eigen(x)
来求矩阵x的特征值和特征向量
> 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
> eigen(X)
eigen() decomposition
$values
[1] 264.12614+ 0.00000i -39.97988+ 0.00000i
[3] 26.02694+12.60671i 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] [,5]
[1,] -0.05950991-0.03631631i -0.5138367+0i
[2,] 0.72745041+0.00000000i 0.2763409+0i
[3,] -0.03767373-0.38696839i 0.6846989+0i
[4,] -0.08196570+0.22750249i -0.3671550+0i
[5,] -0.10038826+0.49622393i -0.2366265+0i
学习R语言的一些途径
- 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.
- The R Journal (http://journal.r-project.org/): A freely accessible refereed journal containing articles on the R project and contributed packages.
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. - Planet R (http://planetr.stderr.org): Another good site-aggregator, including 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!
- Journal of Statistical Software (http://www.jstatsoft.org/): A freely accessible 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.