###################11R的表(求相应组合的频率)###################### age <- c(12, 35, 25, 12, 25) gender <- c("f", "m", "m", "m", "f") table(age,gender) dat<-data.frame(age,gender) table(dat) ###################12基本运算#################### a<-5 b<-3 a%%b#余数 a%/%b#整除 a/b c<--5 abs(c) sign(a)#判断正负 sign(c) sign(0) a^b sqrt(b) log2(2) log10(10) log(4,base = 16) log(exp(2),exp(1)) isTRUE(TRUE) isTRUE(FALSE) identical(1,NULL)#前后是否相等 cumsum(1:5)#累加 cumprod(1:5)#累乘 cumsum(c(1,3,5,7,9)) seq(-3,3)#序列 #unique()找出相异的 a<-c(rep(1,3),rep(2,3),rep(6,7),1:10) a #!!重要!! unique(a) which(a==6)#下标 #%in% ###################13三角运算#################### sin(1)#1表示弧度 a<-c(1:5) sin(a) plot(a,sin(a)) a<-seq(1,10,0.5) plot(a,sin(a)) plot(a,sin(a),type ='line',col='red' ) cos(1) plot(a,cos(a),col=rainbow(length(a))) tan(1) plot(a,tan(a)) plot(a,tan(a),col=rainbow(length(a))) asin(1) atan(2) ###################14解方程#################### #ax+b=0的根(异号) f<-function(x,a,b){return(a*x+b)} uniroot(f,c(-10,10),5,10,tol = 0.0001) root<-uniroot(f,c(-10,10),5,10,tol = 0.0001) root$root f2<-function(x,a,b,d){return(a*x^2+b*x+d)} uniroot(f2,c(-4,-2.55),a=1,b=5,d=6,tol = 0.0011) root2<-uniroot(f2,c(-4,-2.55),a=1,b=5,d=6,tol = 0.0011) root2$root #方程组 f<-matrix(c(3,5,1,2),nrow = 2,byrow = TRUE) f rf<-matrix(c(4,1),nrow = 2) rf solve(f,rf) ###################15-17for、while、repeat循环#################### a<-c('a','b','c') #下标访问 for (i in c(1:length(a))) { print(i) } for (i in c(1:length(a))) { print(a[i]) } #直接访问 for (i in a) { print(i) } #循环嵌套 a<-matrix(c(1:24),nrow = 4) a for (i in c(1:nrow(a))) for (j in c(1:ncol(a))) { print(a[i,j]) } #1到100的和 s<-0 L<-1 while (L<=100) { s<-s+L L<-L+1 } s #e的泰勒级数 e<-1 i<-1 while((1/prod(1:i)-1/prod(1:(i+1)))>0.001){ print(i) e<-e+1/prod(1:i) i<-i+1 } e #repeat s<-0 i<-1 repeat{ if(i>100){ break() } s<-s+i i<-i+1 } s ###################18函数基本知识#################### printTest<-function(x){ print(x) } printTest('hello world!') ret<-function(x,y){ return(x+y) } ret(1,2) #矩阵相乘函数 mat1<-matrix(c(1:12),nrow = 3,ncol = 4) mat2<-matrix(c(1:24),nrow = 4,ncol = 6) f<-function(x,y){ xcol<-dim(x)[2] yrow<-dim(y)[1] m<-dim(x)[1] n<-dim(y)[2] if(xcol!=yrow){ print('the two matrix diamoids is not equal!') return(0) } mat<-matrix(0,nrow = dim(x)[1],ncol = dim(y)[2]) for(i in c(1:m)) for (j in c(1:n)) { mat[i,j] <- sum(x[i,]*y[,j]) } return(mat) } mat1%*%mat2 f(mat1,mat2) mat3<-matrix(c(1:14),nrow = 2,ncol = 7) f(mat1,mat3) ###################19管道函数(%>%,将左边的对象传递给右边函数)#################### #install.packages("dplyr") library(dplyr) f<-function(x,y){ return(x*y) } f(2,4) 2%>%f(4) f2<-function(x,y,z){ return(x*y+z) } f2(2,3,4) 3%>%f2(2,.,4) #例1 f<-function(x){return(x+1)} f2<-function(x){return(x^2)} f3<-function(x){return(sin(x))} x<-4 x %>% f() %>% f2() %>% f3() #例2 #install.packages("tidyr") library(tidyr) date<-as.Date('2020-04-16')+0:19 date hour<-sample(1:24,20) hour minute<-sample(1:60,20) minute second<-sample(1:60,20) second dat<-data.frame(date,hour,minute,second) dat Sys.time() dat %>% unite(dateHour,date,hour,sep = ' ') %>% unite(dateHourMinute,dateHour,minute,sep = ':') %>% unite(dateTime,dateHourMinute,second,sep = ':') dat %>% unite(dateHour,date,hour,sep = ' ') %>% unite(dateTime,dateHour,minute,second,sep = ':') ###################20函数递归#################### fib<-function(n){ if(n==1||n==2){ return (1) } return (fib(n-1)+fib(n-2)) } for (i in c(1:10)) { print(fib(i)) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?