您的位置:首页 > 其它

R 语言语法快速浏览

2016-07-05 22:04 405 查看
1、自定义函数

Huang <- function(n,Mean,Variance,Left_lim,Right_lim,Up_lim,Down_lim,method="Density")   ###括号中为形参,"Huang"为函数名
{
if(method=="Density")
{
set.seed(1)
x <- seq(Left_lim,Right_lim,length.out=n)  ##seq()函数表示生成一个n个值的等差数列,首项为Left_lim,末项为Right_lim
Fei <- dnorm(x,Mean,Variance)  ##产生正态密度分布随机数
my_distribution_y <- exp(Fei)
plot(x,my_distribution_y,col="red",xlim=c(Left_lim,Right_lim),ylim=c(Up_lim,Down_lim),type='l',xaxs="i", yaxs="i",ylab='density',xlab='',main="My own Distribution")
return(my_distribution_y)
}
else if(method=="Distribution")
{
set.seed(1)
x <- seq(Left_lim,Right_lim,length.out=n)
Fei <- rnorm(x,Mean,Variance)  ##产生正态分布随机数
my_distribution_y <- exp(Fei)
plot(x,my_distribution_y,col="red",xlim=c(Left_lim,Right_lim),ylim=c(Up_lim,Down_lim),xaxs="i", yaxs="i",ylab='Distribution',xlab='',main="My own Distribution")
return(my_distribution_y)
}
}
###函数调用
Huang(10000,0,1,-4,4,0,2,"Density")
Huang(10000,0,1,-4,4,0,50,"Distribution")


2、矩阵操作

2.1 创建一个向量

在R中可以用函数c()来创建一个向量,例如:

> x=c(1,2,3,4)
> x
[1] 1 2 3 4


2.2 创建一个矩阵

在R中可以用函数matrix()来创建一个矩阵,应用该函数时需要输入必要的参数值。

> args(matrix)
function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)


data项为必要的矩阵元素,nrow为行数,ncol为列数,注意nrow与ncol的乘积应为矩阵元素个数,byrow项控制排列元素时是否按行进行,dimnames给定行和列的名称。例如:

> matrix(1:12,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,]   1   4   7   10
[2,]   2   5   8   11
[3,]   3   6   9   12
> matrix(1:12,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,]   1   5   9
[2,]   2   6   10
[3,]   3   7   11
[4,]   4   8   12
> matrix(1:12,nrow=4,ncol=3,byrow=T)
[,1] [,2] [,3]
[1,]   1   2   3
[2,]   4   5   6
[3,]   7   8   9
[4,]   10   11   12
> rowname
[1] "r1" "r2" "r3"
> colname=c("c1","c2","c3","c4")
> colname
[1] "c1" "c2" "c3" "c4"
> matrix(1:12,nrow=3,ncol=4,dimnames=list(rowname,colname))
c1 c2 c3 c4
r1 1 4 7 10
r2 2 5 8 11


2.3 矩阵转置

A为m×n矩阵,求A’在R中可用函数t(),例如:

> 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


若将函数t()作用于一个向量x,则R默认x为列向量,返回结果为一个行向量,例如:

> 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
> class(x)
[1] "integer"
> class(t(x))
[1] "matrix"


若想得到一个列向量,可用t(t(x)),例如:

> x
[1] 1 2 3 4 5 6 7 8 9 10
> t(t(x))
[,1]
[1,]   1
[2,]   2
[3,]   3
[4,]   4
[5,]   5
[6,]   6
[7,]   7
[8,]   8
[9,]   9
[10,]  10
> y=t(t(x))
> t(t(y))
[,1]
[1,]   1
[2,]   2
[3,]   3
[4,]   4
[5,]   5
[6,]   6
[7,]   7
[8,]   8
[9,]   9
[10,]   10


2.4 矩阵相加减

在R中对同行同列矩阵相加减,可用符号:“+”、“-”,例如:

> A=B=matrix(1:12,nrow=3,ncol=4)
> 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


2.5 数与矩阵相乘

A为m×n矩阵,c>0,在R中求cA可用符号:“*”,例如:

> c=2
> c*A
[,1] [,2] [,3] [,4]
[1,]   2   8   14   20
[2,]   4   10  16   22
[3,]   6   12  18   24


2.6 矩阵相乘

A为m×n矩阵,B为n×k矩阵,在R中求AB可用符号:“%*%”,例如:

> A=matrix(1:12,nrow=3,ncol=4)
> B=matrix(1:12,nrow=4,ncol=3)
> A%*%B
[,1] [,2] [,3]
[1,]   70  158 246
[2,]   80  184 288
[3,]   90  210 330


若A为n×m矩阵,要得到A’B,可用函数crossprod(),该函数计算结果与t(A)%*%B相同,但是效率更高。例如:

> A=matrix(1:12,nrow=4,ncol=3)
> B=matrix(1:12,nrow=4,ncol=3)
> t(A)%*%B
[,1] [,2] [,3]
[1,]  30   70 110
[2,]  70  174 278
[3,] 110  278 446
> crossprod(A,B)
[,1] [,2] [,3]
[1,]  30  70 110
[2,]  70 174 278
[3,] 110 278 446


矩阵Hadamard积:若A={aij}m×n, B={bij}m×n, 则矩阵的Hadamard积定义为:

A⊙B={aij bij }m×n,R中Hadamard积可以直接运用运算符“*”例如:

> A=matrix(1:16,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
> B=A
> A*B
[,1] [,2] [,3] [,4]
[1,]   1   25   81 169
[2,]   4   36 100 196
[3,]   9   49 121 225
[4,]   16   64 144 256


R中这两个运算符的区别加以注意。

2.7 对矩阵进行某个维度的运算(apply函数)

apply函数是对一个Matrix或者Array进行某个维度的运算。其格式是:

Apply(数据,维度Index,运算函数,函数的参数)


对于Matrix来说,其维度值为2,第二个参数维度Index中,1表示按行运算,2表示按列运算。下面举一个例子:

m<-matrix(1:6,2,3)


构建一个简单的2行3列的矩阵,内容为:

[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6


如果我们要计算每一行的sum值,那么我们可以写为:

apply(m,1,sum)
[1]  9 12


如果要计算每一列的mean值,那么改为:

apply(m,2,mean)
[1] 1.5 3.5 5.5


假如某个值为NA,那么要忽略NA值,进行每一行的SUM怎么办呢?

m[2,2]<-NA
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2   NA    6
apply(m,1,sum)
[1]  9 NA


本身sum函数有一个参数na.rm,我们可以将这个参数带人到apply函数中,作为第4个参数:

apply(m,1,sum,na.rm=TRUE)
[1] 9 8


需要注意的是如果是Data Frame,那么系统会将其转为Matrix,如果所有Column不是数字类型或者类型不一致,导致转换失败,那么apply是运算不出任何一列的结果的。

3 循环

for(name in statement)

{expression}

其中,name为循环,statement一般为一向量,expression一般为组合表达式。

例1:

for(n in c(2,5,10,20,50))
{
x <- rnorm(n)
cat(n,":",sum(x^2),"\n")
}
2:0.4124139
5:5.202875
10:13.96513
20:20.07696
50:58.16209


例2:

norm <- rnorm(100,1,1)
min.norm <- 100
for(i in 1:100)
{
if(norm[i] <= min.norm)
{
min.norm = norm[i]
min.count = i
}
}


while循环与其他编程语言,如C语言类似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: