R语言基本语法
更新一下r语言语法,因为课上要用到。
1.1 变量名命名规则:
R 语言的有效的变量名称由字母,数字以及点号 . 或下划线 _ 组成。
变量名称以字母或点开头。
1.2 变量赋值与处理:
# three naming methods var.1 = c(1,2,3,4) print(var.1) var.2 <- c("hello") print(var.2) c("hello") -> var.3 print(var.3) #show the defined variations and delete 'x' and 'y' print(ls()) rm(x) rm(y) print(ls())
注:
1.3 输入输出:
#input and output print("hello world") cat(1,"add",1,"=",2) #just as in python #cat() 函数支持直接输出结果到文件: cat("text.....", file="C:/Users/xxx/Desktop/R语言工程/learn R/text.txt") #sink() 函数可以把控制台输出的文字直接输出到文件中去: #这条语句执行以后,任何控制台上的输出都会被写入到文件中去,控制台将不会显示输出。 #注意:这个操作也是"覆盖写入"操作,会直接清除原有的文件内容。 #如果我们依然像保留控制台的输出,可以设置 split 属性: sink("C:/Users/xxx/Desktop/R语言工程/learn R/test.txt", split=TRUE) print("test..test...") #如果想取消输出到文件,可以调用无参数的 sink : sink() #从文件读入文字 readLines("C:/Users/xxx/Desktop/R语言工程/learn R/test.txt") #关于工作目录的获取: print(getwd()) # 设置当前工作目录 setwd("xxx") # 查看当前工作目录 print(getwd())
1.4 注释:
if(FALSE) { " 这是一个多行注释的实例 注释内容放在单引号或双引号之间 " }
1.5 数学计算相关函数:
# calculation #赋值 a <- 123 b <- 456 print(a+b) #当然,= 也可以使用 #乘方运算 c = 2^3 print(c) #整除求余数 d = 7%%3 print(d) #整除 d = 7%/%3 print(d) #向右赋值 8 -> e print(e) #创建一系列数字的向量 v <- 1:10 print(v) #判断数字是否在向量v中: v1 <- 3 v2 <- 15 print(v1 %in% v) print(v2 %in% v) #一些基本数学函数: f = sqrt(20) print(f) i = exp(10) print(i) j = log (2,8) print(j) k = log10(10) print(k) #取整函数: print(round(2.56676767)) print(round(2.56676767,4)) print(ceiling(2.5656)) print(floor(2.5656)) #关于三角函数(弧度制表示) print(sin(pi/6)) print(asin(0.5)) #关于概率中的正态分布: print(dnorm(0)) #概率密度函数 print(pnorm(0)) #分布函数值 print(qnorm(0.95))#分位数函数 print(rnorm(3,5,2))#概率仿真,产生三个均值为5,标准差为2的数字
1.6 数据类型:
###vector### a = c(1,2,3,4,5) print(a[1]) #three methods of extracting elements from vector print(a[2:4]) print(a[c(1,3,5)]) print(a[c(-1,-5)]) #alterations: print(sort(a)) print(rev(a)) print(order(a)) print(a[order(a)]) #vector generations print(seq(1,9,2)) print(seq(0,1,length.out = 3)) print(rep(0,6)) #about NA and NULL:NULL is actually nothing print(length(c(NA,NA,NULL,NULL))) a = c(NA,NA,NULL) print(a) #Logical vector a = c(11,12,13) b = a > 12 print(b) #function which() filtrates TRUE in vector #an example: which(b) vector_test =c(1,2,3,4,5,6,7,8,9,10) print(vector_test[which(vector_test>=5 & vector_test<=9)]) #strings: print(toupper("This is an example")) print(tolower("This is an example")) print(nchar("This is an example",type="bytes")) print(nchar("This is an example",type="char")) #print(substr("This is an example"),1,10) #print(substring("This is an example"),6) print(as.numeric("123")) print(as.character(12.34)) print(strsplit("2022;7;19",";")) print(gsub("/", "-", "2022/7/19"))
###matrix### #creation of matrix: #default: cols vector = c(1,2,3,4,5,6,7,8,9) matrix_1=matrix(vector,3,3) print(matrix_1) #rows print(matrix_1[2,3]) #add names: colnames(matrix_1)=c('a','b','c') rownames(matrix_1)=c('e','f','g') print(matrix_1) matrix_2= matrix(vector,3,3,byrow=TRUE) print(matrix_2) #matrix calculations m1 = matrix(c(1,2),1,2) m2 = matrix(c(3,4),2,1) print(m1 %*% m2) #get inverse matrix a = matrix(c(1,3,2,4),2,2) print(a) print(solve(a)) #calculate the cols and rows print(apply(a,1,sum)) print(apply(a,2,sum))
###list### #creation: list_001 <- list("good",123,c(1,2,3),"bad",12.32) print(list_001) names(list_001)<-c("string","number","vector","string","float") print(list_001) #visit the lists: print(list_001[3]) print(list_001[4]) print(list_001$number)#use the names to find elements #add: list001<-"new elements" #delete list_001[2]<-NULL #merge the lists: list_002 <- list("list002_1","list002_2","list002_3") list_003 <- c(list_001,list_002) print(list_003) #turn the lists into vetctor: v1 <- unlist(list_001) v2 <- unlist(list_002) print(v1) print(v2)
1.7 判断语句和循环语句:
#if ... else #switch a <- 4 x <- switch( a, "a", "b", "c", "d", "e" ) print(x) #if no in range ,return NULL
#three ways: repeat,while,for #001 repeat:never get out until TRUE or break n <- 1 repeat{ print(1) n <- n+1 if(n>5){ break } } #002 while while(i<7) { print(2) i <- i+1 } #003 for v <- LETTERS[1:4] for(i in v) { print(3) } #and the control sentence: next and break
1.8 函数
#creation: i <- 0 function001 <- function(data1,data2) { repeat{ i <- i+1 print("test...") if(i>=(data1+data2)) { break } } for (i in 1:data1) { print("ha") } } function001(3,1) #R is lazy and the following is'nt error newfunction <- function(x1) { 200 } print(newfunction(20))
1.9 关于数据框:
#create a table table = data.frame( name = c ("Mr.K","Mr.M","Mr.G"), salary = c(30000,45000,60000), work = c("001","002","003") ) print(table) #show the data structure of table: print(str(table)) #show the further analysis of the table: print(summary(table)) #get the cols of the table: result <- data.frame(table$name,table$salary) print(result) #print the former two rows: result <- table[1:2,] print(result) #add information table2 = data.frame( name = c("skeleton","steve","creeper"), damage = c(10,10,100) ) table2$cost<-c("20","25","50") print(table2) #stick all the vectors into a new table: name <- c("farmer","strong farmer","hunter") costs <- c(10,15,25) heart <- c(20,35,15) damage <- c(5,5,20) table3 <- cbind(name,costs,heart,damage) print(table3)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App