1.eigen(x,symmetrics,only.values = FALSE)求解矩阵的特征值,x为求解矩阵,symmetrics表示是否为对称阵,only.values = TRUE,只返回特征值,否则返回特征值和特征向量。
> eigen(a,only.values = FALSE)
$values
[1] 3.620937e+01 -2.209373e+00 -1.050249e-15 8.203417e-16
$vectors
[,1] [,2] [,3] [,4]
[1,] -0.4140028 -0.82289268 0.4422036 -0.1001707
[2,] -0.4688206 -0.42193991 -0.3487083 0.5349238
[3,] -0.5236384 -0.02098714 -0.6291942 -0.7693354
[4,] -0.5784562 0.37996563 0.5356989 0.3345823
> eigen(a,only.values = TRUE)
$values
[1] 3.620937e+01 -2.209373e+00 -1.050249e-15 8.203417e-16
$vectors
NULL
2.svd()函数可对矩阵进行奇异值分解
> a<-array(c(1:16),dim = c(4,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
> svd(a)
$d
[1] 3.862266e+01 2.071323e+00 1.291897e-15 6.318048e-16
$u
[,1] [,2] [,3] [,4]
[1,] -0.4284124 -0.7186535 0.5462756 -0.0397869
[2,] -0.4743725 -0.2738078 -0.6987120 0.4602190
[3,] -0.5203326 0.1710379 -0.2414027 -0.8010772
[4,] -0.5662928 0.6158835 0.3938391 0.3806452
$v
[,1] [,2] [,3] [,4]
[1,] -0.1347221 0.82574206 -0.4654637 -0.2886928
[2,] -0.3407577 0.42881720 0.4054394 0.7318599
[3,] -0.5467933 0.03189234 0.5855124 -0.5976414
[4,] -0.7528288 -0.36503251 -0.5254881 0.1544743
3.cut函数划分数据分布区间,table函数统计数据在每个区间出现的频率。
> y<-c(11,22,13,14,11,22,31,31,31,14)
> cut(y,5)->cuty#划分到5个分布区间
> cuty
[1] (11,15] (19,23] (11,15] (11,15] (11,15] (19,23] (27,31] (27,31] (27,31] (11,15]
Levels: (11,15] (15,19] (19,23] (23,27] (27,31]
> table(cuty)
cuty
(11,15] (15,19] (19,23] (23,27] (27,31]
5 0 2 0 3
#用hist函数生成分布直方图
> bins<-seq(min(y),max(y),by=4)
> hist(y,breaks=bins,col="lightblue",axes=FALSE)
> axis(1,bins)
> axis(2)
4.自定义函数计算N维向量的模长。
> N_vector_length<-function(x){
+ temp<-0
+ for (i in 1:length(x)){
+ temp<-temp+x[i]^2
+ }
+ vlength<-sqrt(temp)
+ vlength
+ }
> N_vector_length(c(11,22,33,44,55))
[1] 81.57818