R语言入门基础

教程:常用运算函数。对一般数据进行运算的常用函数:

1、round() #四舍五入
例:x <- c(3.1416, 15.377, 269.7)
round(x, 0) #保留整数位
round(x, 2) #保留两位小数
round(x, -1) #保留到十位
 
2、signif() #取有效数字(跟学过的有效数字不是一个意思)
例:略
 
3、trunc() #取整
   floor() #向下取整
   ceiling() #向上取整
例:xx <- c(3.60, 12.47, -3.60, -12.47)
trunc(xx)
floor(xx)
ceiling(xx)
 
4、logb(a, b) #以b为底的对数,省略b表示自然对数
   log() #自然对数
   log10() #以10为底的常用对数
例:logb(8, 2)
log(8); logb(8)
log10(100); logb(100, 10)
 
5、sqrt() #平方根
   exp() #指数
 
6、sin() #正弦
   cos() #余弦
   tan() #正切
   asin() #反正弦
   acos() #反余弦
   atan() #反正切
   sinh() #双曲正弦
   tanh() #双曲正切
 
7、nchar() #字符长度
例:xx <- 'China is a great country'
nchar(xx)
 
8、substring() #取子字符串
例:substring(xx, 1, 5)
 
9、paste() #连接字符
语法是:paste(..., sep = " ", collapse = NULL)
例1:x <- 'I'; y <- 'am'; z <- 'a'; d <- 'student'
paste(x, y, z, d)
例2:paste(c('x', 'y'), 1:4, sep = '')
例3:paste('x', 1:4, sep = '', collapse = '+')
 
原文地址:https://www.cnblogs.com/wuzhitj/p/4743561.html