R语言学习笔记【5】输入输出与其他编程问题

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值