#chapter5 #### rm(list = ls()) round(333333.141592653,digits = -5) # x<-rnorm(1:9) x round(x,digits = 2) abs(-0.2) sqrt(c(64,81)) #开平方 ceiling(pi) #上限(天花板) pi floor(pi) #对应ceiling trunc(5.99) #truncate ,从整数部分截断 signif(pi,digits = 2) #两位有效数字 round(pi,digits = 2) cos(60) cos(0) sin(30) (sqrt(2))/2 tan(90) acos(1) asin() atan() log(1,base = 10) log(1000,base = 10) log10(1000) log(x) # = log(x,base=e) exp(2) #2.718282^2 2.718282^2 exp(c(1,1,2))# exp(c(1,1,2)) important sqrt(c(4,9,16)) x <- c(1:10,rep(20,10),rep(1,10)) z<-mean(x,trim = 0.2,na.rm = TRUE);z #trim,去掉了最大/小的20%的数,30*20%=6,去掉6个20,6个1,(55+4*20+4*1)/18=7.7222 x=0 for (i in 1:10) { x=x+i print(x) } median(x) sd(x) ?var(x) y<-quantile(x,c(.25,.75)) #取百分之25和75位数 y range(x) diff(range(x)) sum(x) sum(c(1,2,3,4,5)) x diff(x,lag = 6) # 第七个数减去第一个数 x min(x) max(x) #list5-1#### x<-c(1:8) mean(x) sd(x) n<-length(x) meanx<-sum(x)/n meanx css<-sum((x-meanx)^2) sd<-sqrt(css/(n-1)) sd mydata<-c(1,1,1,2,2,3,2,2,1,1,1) newdata<-scale(mydata)*3+0.1 #normalization *sd + mean newdata<-scale(mydata);newdata mean(newdata) sd(newdata) x<-pretty(c(-3,3),20) ;x #pretty from-3 to 3 ,平均分成30份 y<-dnorm(x);y #表示在该正态分布下,x对应的曲线的函数值 可用于作图 plot(x,y,type = "l",xlab = "Normal Deviate",ylab = "Density",yaxs="i") plot(x,y,type = "l",xlab = "Normal Deviate",ylab = "Density") pnorm(1.96) pnorm(q = 3.84,mean = 0,sd = 1) #probability 该正态分布下,x为3.84时曲线下右侧的面积 qnorm(.75,mean = 500,sd = 100) #quantity 该正态分布下,曲线下右侧面积为0.75时X的值 x<-rnorm(50,mean = 80,sd = 20);x #生成50个,均数80,sd20的正态分布的样本 #list 5-2 runif(5, min = -1, max = 1) ## sample random number runif(5) runif(5) runif(5) set.seed(1234) runif(5) set.seed(1234) runif(5) #list 5-3#### install.packages("MASS") library(MASS) options(digits = 4) ## set.seed(1234) mean<-c(230.7,146.7,3.6) sigma<-matrix(c(15360.8,6721.2,-47.1, 6721.2,4700.9,-16.5, -47.1,-16.5,0.3),nrow = 3,ncol = 3) sigma mydata<-mvrnorm(500,mean,sigma);mydata #generate multivariate normal sequence assigned mean and covariance matrix mydata<-as.data.frame(mydata) names(mydata)<-c("y","x1","x2") dim(mydata) head(mydata) head(mydata, 10) mean(mydata$y) #5.2.4 handle character string #### x<-c("ab","aacde","fghqaij") length(x) nchar(x[3]) #number of character nchar(x) substr(x[3],5,6)<-"35" # R object start stop substitute x x <- c(‘abadfa‘) substr(x,1,5) <- "nnnnnxn" substr(x,1,5) <- "nnnnxxn" x x <- c("The Great Well") x <- chartr("[A-Z]","[a-z]",x);x x <- chartr("we","wa",x);x # w-w,e-a x <- chartr(‘a‘,‘ ‘,x);x x <- gsub(‘a‘,‘‘,x);x x <- gsub("Th","",x);x ##!!!!!!!!!!!! aa<-grep(pattern,x,ignore.case = FALSE,fixed = FALSE) #其中pattern是regular expression grep(pattern,x,ignore.case = FALSE,fixed = TRUE) #正则表达式 pattern<-"[a]" sub(pattern = "[a]","@",x,ignore.case = FALSE,fixed = FALSE) #substitute sub("[^aa]","@",x,ignore.case = FALSE,fixed = FALSE) #fixed= FALSE,pattern is regular expression, #or is character string.txt x <- c(a="2018Dj12TB17HU",b="2017Dj12TB17HU") x <- read.table("clipboard") v1 <-as.character(x$V1) y <- strsplit(v1,"Dj") strsplit("a.b.c",".") # .在reg exp中表示任意字符,所以想表示. 用[.] strsplit("a.b.c","[.]") strsplit("a.b.c",".",fixed = T) simpleCap <- function(x) { s <- strsplit(x, " ")[[2]] paste(toupper(substring(s, 1, 1)), substring(s, 2), sep = "", collapse = " ") } # paste 默认增加一个space在字符之间,paste0相当于paete(...,sep=‘‘) paste0(1:3,c(‘a‘,‘a‘,‘a‘)) paste(1:12) as.character(1:12) paste("ID",1:5,sep = "/",collapse = "!") #collapse表示用什么断开字符串 paste("ID",1:5,sep = "/") nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9)));nth #replicate("sth.",times) nth <- paste(1:12, c("st", "nd", "rd", rep("th", 9)),sep = ‘‘,collapse = ‘ ‘);nth paste(month.abb, "is the", nth, "month of the year.") paste("today is","a good day") paste("today is","good day",sep = " a ") toupper("today") tolower("Today") install.packages("Himsc") library(‘Himsc‘) y<-"ajfhajnac" capitalize(y) capitalize(c("Hello", "bob", "daN")) #单独大写initial x<-c(1,2,3,4) length(x) seq(1,10,2) # from,to,by rep(1:3,6) #rep(x,n) cut(x,3) cut(x,3,ordered_result = TRUE) pretty(1:3,10) name<-"Bob" cat("Hello","bob","\b.\n","Isn\‘t R","\t","GREAT?\n") cat("Hello",name,".\n","Isn\‘t R","\b","GREAT?") ?Quotes #5-4 a<-5 sqrt(a) b<-c(1.23,4.4,81.5323) round(b) set.seed(23) c<-matrix(runif(12),nrow = 3) c mean(c) x<-matrix(rnorm(30),nrow = 5) x apply(x,1,mean) #1 indicates rows,2 columns apply(x,2,mean) apply(x,2,mean,trim = 0.2)
原文:https://www.cnblogs.com/super-yb/p/11046601.html