ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

R语言基本语法

2022-07-21 10:36:17  阅读:292  来源: 互联网

标签:基本 语言 语法 test vector print table ### matrix


更新一下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)

 

标签:基本,语言,语法,test,vector,print,table,###,matrix
来源: https://www.cnblogs.com/MrMKG/p/16492016.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有