STA3050 Lec5笔记
定义一个函数
> "%+-%" <- function(x,s) { c(x-s,x+s) }
> 3 %+-% 5
[1]-2 8
z<-outer(y,x,"+")
# save the table to z, where z(i,j)=y(i)+x(j)
输出数字格式:
> sprintf("%f", pi)
# output real number with default option = 6 decimal places
[1] “3.141593” #默认小数点后6位
> sprintf("%.3f", pi)
[1] “3.142” #小数点后三位
> sprintf("%5.1f", pi) #总长度5位,小数点后1位,小数点占一位,之前不足用空格,证书超过3位都保留
[1] " 3.1"
> sprintf("%-10f", pi)
[1] “3.141593 " #同上,带-号是左对齐,右边加空格
> sprintf(”%e", pi) # scientific notation
[1] "3.141593e+00“
cat()函数
cat("XXXX", "\n") #\n是换行的意思
> a<-c(1,2,3,4)
> b<-c(4,5,6,7)
> cat(a,"\n", b,"\n")
1 2 3 4
4 5 6 7
读取输入
inp<-readline()
计算运行时间
pt0<-proc.time()
……
proc.time()-pt0
outer函数
outer(x,y,paste或"operator")
e.g.
A<-matrix(1:16, nrow=4)
# create a square matrix A outer(1:nrow(A),1:nrow(A),'>=')*A
# change upper triangular elements to zero
[,1][,2][,3][,4]
[1,] 1 0 0 0
[2,] 2 6 0 0
[3,] 3 7 11 0
[4,] 4 8 12 16
slash(斜线)函数
> slash(matrix(1:20,ncol=4))
[,1][,2][,3][,4]
[1,] 0 0 0 16
[2,] 0 0 12 0
[3,] 0 8 0 0
[4,] 4 0 0 0
[5,] 0 0 0 0
sort()
readline()
traceback()