小白学习R语言——R函数

来源:互联网 发布:java message类 编辑:程序博客网 时间:2024/05/17 07:46

1,赋值运算符:<-,=。

a,向量

表示:c()

            seq/length

运算:%/%(整除),%%(求余),sqrt/log/exp/sin/cos/tan 对向量里的每个元素分别运算

            min/max/sum/var(方差)/sd(标准差)/mean/median/range(最大和最小值)/sort(从小排序)/order(从小到大下标)

注:var:平方的均值减去均值的平方。

b,矩阵

表示:matrix/rbind/cbind

运算:crossprod(X,Y)(内积),%*%,solve(解线性方程),svd/apply(如下)

> apply(x,1,sum)
[1] 10 26 42 58
> apply(x,1,mean)
[1]  2.5  6.5 10.5 14.5


c,数组:定义dim属性的值

表示:array(x,dim=) 

            dim(z)<-(2,3,4) #4是dim

运算:outer(a,b,f)


d,因子

表示:factor

运算:table(频数统计)/tapply/gl


e,列表与数据框

表示:列表 list    (list $ name,list[])

            数据框:矩阵形式,但列可以不同数据类型。data.frame ([[]],$)

运算:ls()


mark 几张简单的图


layout(matrix(c(1,1,1,2,3,4,2,3,4),nr=3,byrow=T)) nr=3意思是分为3列

2,布朗运动及平滑曲线

n<-100
x<-cumsum(rnorm(n))
y<-cumsum(rnorm(n))
plot(x,y)
sp<-spline(x,y)
lines(sp)

3,把数值矩阵映射为颜色方格矩阵

x<-y<-seq(-10,10,length=50)
f<-function(x,y){
                  r<-sqrt(x^2+y^2)
                  10*sin(r)/r
}

#求外积
z<-outer(x,y,f)
image(x,y,z)
filled.contour(x,y,z)

4,散点图中散点大小同因变量值成比例如何画

x<-1:10

y<-runif(10)

symbols(x,y,circles = y/2,inches = F,bg=x)


0 0