1. 各种分布的随机数生成函数
rnorm(n, mean = 0, sd = 1) 正态分布
rexp(n, rate = 1) 指数
rgamma(n, shape, rate = 1, scale = 1/rate) r分布
rpois(n, lambda) 泊松
rt(n, df, ncp) t 分布
rf(n, df1, df2, ncp) f 分布
rchisq(n, df, ncp=0) 卡方分布
rbinom(n,size,prob) 二项
rweibull(n,shape,scale=1) weibull分布
rbata(n,shape1,shape2) bata 分布
2. grep,gsub的区别
简单的来说grep是查找位置,gsub是替换匹配的字符(串)
例如:
txt <-c("arm","foot","lefroo", "bafoobar")
grep("foo",txt)
gsub("foo","ggg", txt)
3. 全排列
library(e1071)
permutations(5)
4. 绘制椭圆
library(plotrix)
plot(c(0,10), c(0,10), type="n",main="test draw.ellipse")
draw.ellipse(5, 5, 4,2,border='red',col='lightblue')
5. 曲线图添加阴影
A <-rnorm(100000, mean=2, sd=0.7)
B <-rnorm(100000, mean=4, sd=0.7)
plot(density(A),xlim = c(0,7), lwd=2, col="blue")
lines(density(B),lwd=2, col="red")
p=density(B)
q=density(A)
px=p$x;
py=p$y;
qx=q$x;
qy=q$y;
wp=which(px<4&px>2)
wq=which(qx<4&qx>2)
polygon(c(px[px<4&px>2],4),c(py[wp],qy[qx>4][1]), col= "orange", lty = 2, lwd = 2, border = "red")
polygon(c(2,qx[qx<4&qx>2]),c(py[px>2][1],qy[wq]), density= c(10, 20), lty = 2, lwd = 2, border = "red")
6. 字符个数和字符串长度
nchar统计字符个数的函数.
strwidth 应该是相对字节所占用的长度,跟个数无关。
例如 nchar(as.vector(aaa[, 2]))
[1] 33 2
strwidth(as.vector(aaa[, 2]))
[1] 0.47460940.4746094 0.5625000
如果要看字节的话,字符的话
nchar 的 type参数"char","byt..","width..."3个参数
一个汉字(一个字符)占两个字节,字母(一个字符)占一个字节。
直接这样写好像需要打开plot
不过可以这样 strwidth(as.vector(aaa[,2]),"inches") #inches 英寸
7. 坐标轴加上箭头
curve(x^3+5*x,from=-4,to=4)
arrows(x0=0,y0=-90,x1=0,y1=90,length=0.17)
arrows(x0=-4.2,y0=0,x1=4.2,y1=0,length=0.17)
8. 画图形的颜色填充条
library(fields)
library(spam)
data( RCMexample)
image.plot(RCMexamplex,RCMexampley, RCMexample$z[,,1])
x<- 1:20
y<- 1:15
z<- outer(x,y,"+")
zr<- range(c(z))
image.plot(zlim=zr, legend.only=TRUE, horizontal=TRUE, legend.mar=5)
9. 用XML包从网站上读表格
library(XML)
myurl <-"http://en.wikipedia.org/wiki/List_of_heights_of_United_States_presidential_candidates"
tables <-readHTMLTable(myurl)
n.rows <-unlist(lapply(tables, function(t) dim(t)[1]))
yourtable <-tables[[5]]
10. 读取压缩文件
read.table(pipe("links-source http://www.mayin.org/ajayshah/A/demo.text.bz2 | bunzip2"))
load(url("http://www.mayin.org/ajayshah/A/nifty_weekly_returns.rda"))
11. 如何知道变量所在列数
用names函数与用colnames函数的结果是一样的,楼上提供的表达式中,z需要加引号
即 which(colnames(data)=='z')
或 which(names(data)=='z')
12. 在3D图形中加一个圆或椭圆平面
# x and y followindependent normal
sd.x <- 1
sd.y <- 2
x <-c(-50:50/10)*sd.x
y <-c(-50:50/10)*sd.y
z <-outer(dnorm(x, sd = sd.x), dnorm(y, sd = sd.y))
# Target z value(half of the maxium)
z.tgl <-max(z)/2
# Inverse of dnorm(x > 0 part)
inv.dnorm<- function(d, mean = 0, sd = 1)
{
return(mean + sqrt(-2*sd*sd*log(sqrt(2*pi)*sd*d)))
}
# Half range
hlfrng.x<- seq(0, inv.dnorm(z.tgl/max(dnorm(y, sd = sd.y)), sd = sd.x),length.out = 51)
hlfrng.y<- inv.dnorm(z.tgl/dnorm(hlfrng.x, sd = sd.x), sd = sd.y)
# Target x, yvalue
x.tgl<- c(hlfrng.x, rev(head(hlfrng.x, -1)), -1*head(hlfrng.x[-1],-1), rev(-1*hlfrng.x[-1]))
y.tgl<- c(hlfrng.y, rev(-1*hlfrng.y)[-1], -1*head(hlfrng.y[-1], -1),head(rev(hlfrng.y), -1))
# Assert correctlength
stopifnot(length(x.tgl)== length(y.tgl))
# Normal surface
persp(x, y, z,theta = 30, scale = T, box = F, axes = F, zlim = c(0, 0.15), border ="green4") -> res
# Eclipse
polygon(trans3d(x.tgl,y.tgl, rep(z.tgl, times = length(x.tgl)), res), border = "Navy", col= "#00007F3F")
Result:
13. 改变坐标轴标签方向
plot(1,1,las=1)
14. 执行外部程序打开文件
shell.exec('C:\\test.txt')
15. 加标签的气泡图
attach(mtcars)
r <-sqrt(disp/pi)
symbols(wt, mpg,r, inches=0.30, fg="white", bg="lightblue",
main="BubblePlot with point size proportional to displacement",
ylab="MilesPer Gallon",
xlab="Weightof Car (lbs/1000)")
text(wt, mpg,rownames(mtcars), cex=0.6)
detach(mtcars)
par(opar)
inches是比例因子,控制着圆圈大小(默认最大圆圈为1英寸)。
text()函数是可选函数,此处用来添加各个汽车的名称。
本文深入探讨了R语言中生成各类分布随机数、使用grep和gsub进行字符串操作、全排列计算、绘制椭圆及曲线图阴影添加、字符个数与字符串长度分析、坐标轴箭头添加、图形颜色填充条制作、从网站读取表格、读取压缩文件、变量所在列数查询、3D图形中加圆或椭圆平面、坐标轴标签方向改变、外部程序文件打开、标签图气泡添加等高级统计与绘图技巧。

2341

被折叠的 条评论
为什么被折叠?



