> (a <- 1:5)
[1] 1 2 3 4 5
> (b <- 2:6)
[1] 2 3 4 5 6
> (c <- 2:3)
[1] 2 3
> (d <- 2)
[1] 2
> a*b
[1] 2 6 12 20 30
> a*c
[1] 2 6 6 12 10
警告信息:
In a * c : 长的对象长度不是短的对象长度的整倍数
> a*d
[1] 2 4 6 8 10
> a <- 2
> switch(a, log(a), a+3, a-3) #a=2,执行列表第二项:a+3
[1] 5
> a <- 1
> switch(a, log(a), a+3, a-3) #a=1,执行列表第一项:log(a)
[1] 0
> a <- "agi"
> switch(a, gi=1030567, agi="AT1G10010", name="CBFx") #返回a(名称)对应的列表向量值
[1] "AT1G10010"
> a <- 1:5
> b <- NULL
> for(i in a) {b <- c(b,log(i))}
> b
[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379
> b <-log(a)
> b
[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379
bar.plot <- function(y1, y2, x=1:length(y1),
sd1=y1*0.05, sd2=y2*0.05,
xlab="Sample", ylab="Level",
labels=c("S1","S2")){
the.data <- rbind(y1, y2)
par(mar=c(3,3,0.5,0.5))
par(mgp=c(2,0.5,0))
pos <- barplot(the.data, ylim=c(0, max(y1,y2)*1.2),
offset=0, axis.lty=1, beside=TRUE,
names.arg = x, col=c("orange","red"))
legend("topleft", legend=labels, fill=c("orange","red"),box.col="white", inset=0.02)
title(xlab=xlab, ylab=ylab)
bw <- 0.2
segments(pos[1,], y1-sd1, pos[1,], y1+sd1, lwd=2)
segments(pos[1,]-bw, y1+sd1, pos[1,]+bw, y1+sd1, lend=2)
segments(pos[1,]-bw, y1-sd1, pos[1,]+bw, y1-sd1, lend=2)
segments(pos[2,], y2-sd2, pos[2,], y2+sd2, lwd=2)
segments(pos[2,]-bw, y2+sd2, pos[2,]+bw, y2+sd2, lend=2)
segments(pos[2,]-bw, y2-sd2, pos[2,]+bw, y2-sd2, lend=2)
box()
}
> source("d:/mybar.R")
> NF <- c(17.44 , 2.56 , 2.70 , 18.71 , 5.61 , 32.98)
> CA <- c(11.48 , 0.75 , 1.16 , 12.73 , 2.84 , 20.04)
> sd.NF <- c(1.27 , 0.15 , 0.48 , 2.01 , 0.80 , 4.09)
> sd.CA <- c(1.09 , 0.36 , 0.11 , 1.82 , 0.92 , 2.36)
> x <- paste("S", 1:length(NF), sep="")
> bar.plot(x=x, y1=NF, y2=CA, sd1=sd.NF, sd2=sd.CA, labels=c("NF", "CA"))
> source("d:/mybar.R")
> set.seed(1000)
> y1 <- abs(rnorm(10))*10
> y2 <- abs(rnorm(10))*5
> bar.plot(y1,y2)