R语言基础编程技巧汇编 - 2

来源:互联网 发布:怎么出售淘宝店铺页面 编辑:程序博客网 时间:2024/06/05 06:59

16.       将R界面改回英文?

到安装目录下的etc文件夹下找到Rconsole文件,打开找到这两行(在末尾):

## Language for messages

language = 

language设置为en即可。若要改回中文,用zh_CN即可。

 

或者:

工具栏中,编辑->GUI选项-Language for messages language =en -Save在指定位置-〉重新运行程序,也是一样的

 

 

17.       控制直方图的bar数目

library(MASS)

truehist(x, nbins=10)

x<-rnorm(100)

hist(x,breaks=seq(min(x),max(x),length=11))

 

breaks参数就可以解决这个问题,需要nbar,就把length参数的取值设为n+1就可以了。

18.       引用文件目录的设置

使用getwd()函数得到当前的工作目录

然后把自己的文件放到这个目录下就可以直接引用了。

 

19.       随机排序(抽样)

x<-1:10

sample(x,10)

 [1]  1  9  5  7 10  4 8  3  2  6

sample(x,10)

 [1]  5  1  3  2  8 10 9  4  6  7

只要第二个参数的数值和第一个参数的长度相同就可以了。

 

 

20.      隐函数作图

例一:

x<-y<-seq(-1,1,0.1)

f<-function(x,y){sin(x)*cos(y)+x+y}

z<-outer(x,y,f)

contour(x,y,z)


例二:

implicitplot<-function(f,xrange=seq(-2,2,length=1000),yrange=seq(-2,2,length=1000)){

  z<-outer(xrange,yrange,f)

 contour(xrange,yrange,z,levels=0,labels='',labcex=.0001)

}

f<-function(x,y) (x^2+y^2-1)^3-x^2*y^2 -(x^2*y^3)

implicitplot(f)


21.       一张图中画多个概率密度函数

set.seed(1234)

x <- rnorm(100)

y <- rnorm(100)

plot(x,y)

lines(density(x))

lines(density(y))



22.       一次运行得到多张图表

运行的结果想要得到多张图表,但总是前一张图被后一张图覆盖,如果是想在屏幕上显示多个图形,在windows系统中只能在plot(...)后输入 windows()重新生成一个图形窗口,然后就可以 plot(...)了,然后再windows()依次……,例如

plot(0)

windows()

plot(0)

windows()

plot(0)

 

如果输出到文件中,则没有这个问题,比如生成 .png文件,png()后,你就可以画图了,然后就可以看到 Rplot001.png, Rplot002.png...

另外,还有三种方法可以尝试:

1.设置par(new=T),这样第二张图就不会覆盖第一张;

2.设置par(mfrow=c(2,2)),把绘图区域分成四小块,图形依次绘入小块之中;

3使用低级绘图函数,第一个用plot,其余的用lines()text(), points()等在图形上添加图形元素,建议使用第三种,可以对图形精确控制。

 

例如,下列程序会在已有图形中绘制文字:

pdf(file="R colors show.pdf",width=13,height=13)

par(col.main="blue")

x<-c(rep(1:8,82),1)

y<-c(rep(83:2,rep(8,82)),1)

texts<-colors()

plot(0,0,type='n',axes=F,xaxt='s',yaxt='s',xlab='',ylab='',xlim=c(1,8),ylim=c(1,83),main="colors()")

text(x,y,texts,col=colors())

dev.off()

 

运行以上的代码,然后当前工作目录下找R colors show.pdf文件。

23.       有放回的随机抽样

e里随机有放回地取N数,形成一个向量。

 

e=c(1:6)

N=100

s=sample(e,N,replace=TRUE)

table(s)

 

24.       在图上用text函数标出点的坐标

x<-1:10

y<-rpois(10,5)

plot(x,y)

text(x,y,paste("(",x,",",y,")"),pos=1,cex=0.7)


 

25. 设定输出数据的小数点位数

options(digits=n)

n为位数

 

round(x, digits = 0)

signif(x, digits = 6)

 

26. 随机从矩阵(数据框)中选取一部分对象

col.name=colnames(mtcars)

row.name=rownames(mtcars)

sam.col.name=sample(col.name,10,replace=FALSE)#列名向量不返回抽样

sam.row.name=sample(row.name,10,replace=FALSE)

B=mtcars[sam.row.name,sam.col.name]

27. 查看str函数返回的某个分量

hc <- hclust(dist(USArrests), "ave")  #运行hclust函数返回一个对象hc 

bbb=str(hc)

List of 7

 $ merge      : int [1:49, 1:2] -15 -17 -14 -13 -35 -36-7 -19 -49 -50 ...

 $ height     : num [1:49] 2.29 3.83 3.93 6.24 6.64 ...

 $ order      : int [1:50] 9 33 5 20 3 31 8 1 18 13 ...

 $ labels     : chr [1:50] "Alabama""Alaska" "Arizona" "Arkansas" ...

 $ method     : chr "average"

 $ call       : language hclust(d = dist(USArrests),method = "ave")

 $ dist.method: chr"euclidean"

 - attr(*, "class")= chr"hclust"

 

hc$merge

      [,1] [,2]

 [1,]  -15 -29

 [2,]  -17 -26

 [3,]  -14 -16

 [4,]  -13 -32

 [5,]  -35 -44

 [6,]  -36 -46

 [7,]   -7 -38

 

看清楚str(bbb)后个分量的名字,然后使用hc$merge即可。

28. 对ts作图时,在x轴上显示详细时间点

x<-ts(1:30,start=c(1900,1),frequency=4)

plot(x,type='l')

axis(1, at = c(1900, 1901, 1902, 1903,1904,1905,1906),labels = expression(1900, 1901, 1902, 1903, 1904,1905,1906))

 

 

 

或者,

plot(x)

axis(1, at=1900:1907)

axis(1, at=seq(1900, 1908, by=1/4), labels=FALSE, tcl=-1/4)



其中,较长tick代表的第一季度1st Quarter短的tick 第二三四季度。

29. 使用combn或expand.grid实现抽取所有可能样本

总体为1234,要从中抽取2个作为样本,如果重复抽样就有16种可能,如果不放回不重复就有6种,而sample函数只能抽取其中的一个样本,不能选出全部样本。

 

1.使用组合函数combn

combn(4, 2)

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

[1,]    1    1   1    2    2   3

[2,]    2    3    4    3   4    4

 

2.或者使用expand.grid

expand.grid(x = 1:4,y = 1:4)

   x y

1  1 1

2  2 1

3  3 1

4  4 1

5  1 2

6  2 2

7  3 2

8  4 2

9  1 3

10 2 3

11 3 3

12 4 3

13 1 4

14 2 4

15 3 4

16 4 4

30. 使用identify函数选取图中对象

require(graphics) 

hca <- hclust(dist(USArrests)) 

plot(hca) 

(x <- identify(hca)) ## Terminate with 2ndmouse button !!

 

朝图中点鼠标即可选取对象,点击Esc键结束。

0 0
原创粉丝点击