mean(1:5)
1:5+6:10
c(1,3,6,10,15)+c(0,1,3,6,10)
sum(1:5) #
c(2,3,5,7,11,12)-2
-2:2*-2:2
1:10/3
1:10%/%3#整数除法
1:10%%3
factorial(5)#阶乘
c(3,4-1,1+1+1)==3 #==是否相等
1:3!=3:1#!=是否不等
(1:5)^2>=16 #1到5的平方是否大于16
sqrt(2)^2==2#2的平方根再平方是否等于2
x<-1:5
y<-6:10
x+2*y-3
x<<-sqrt(4)#全局变量赋值
assign("my_local_variable",9^3+10^3)#第一个参数是变量名,第二个参数是赋的值 globalenv()全局赋值
#第一个参数是变量名,第二个参数是赋的值 globalenv()全局赋值,不会检查变量值是否合法
#特殊数值 inf -inf 无穷大 负无穷大 NaN不是一个数字 NA缺失值
c(Inf+1,Inf-1,Inf-Inf)
setwd("D:/Rdata/Study")
#逻辑向量 !非 &与 |或
(x<-1:10>=5)
(y<-1:10%%2==0)
x&y
x|y
#类的概念
class(TRUE) #numberic大部分数据 logical逻辑值 变量类名
#数值类型 浮点值numberic 整数值integer 复数complex
sqrt(1:10)
class(sqrt(1:10)) #添加L后缀变整数
#存储文本character 存储类别因子factor
class(c("she","sells","seashells"))
gender<-factor(c("male","female"))
levels(gender)
gender<-factor(c("male","male","female","female"))
nlevels(gender)
as.integer(gender) #默认情况下因子水平按子母顺序排列
#检查和更改类
if(is(x,"factor"))
is.character("red lorry,yellow lorry")
is.list(list(a=1,b=2))
is.logical(FALSE)
is(gender,"factor")
#is(pattern="^is",bassenv())
x<-"123.456"
class(x)
as(x,"numeric")
as.numeric(x)
y<-"789.456"
class(y)<-"numeric"
y
#检查变量及工作区
#print查看变量内容 summary查看变量汇总信息 str显示对象结构 unclass显示变量内部构建 attribute对象的所有属性
1+1+1
print(1+1+1)
ulams_spiral<-c(1,8,23,46,77)
for(i in ulams_spiral) print(i)
for(x in ulams_spiral) print(x)
num<-runif(30)
summary(num) # 1stqu. 3rdqu. 四分之一分位数 前四分之三分位数
str(num)
fac<-factor(sample(letters[1:5],30,replace = TRUE))
unclass(fac)
attributes(fac)
attributes(num)
ls()
peach<-1
plum<-"fruity"
pear<-TRUE
ls(pattern = "ea")
rm(peach,plum,pear)
rm(list = ls()) #先用ls查找工作区的所有变量放到list里面再删掉rm
#向量 矩阵 数组
c(1:3,c(5:8),13)
#vecor函数能创建一个指定类型和长度的向量
vector("numeric",5)
numeric(5)
vector("complex",5)
vector("logical",5)
vector("character",5)
seq.int(3,12)#冒号 创建序列 seq.int(开始值,结束值,指定步长)
seq.int(3,12,2)
seq_len(100)
pp<-c("peter","piper","a","peck","of","pickeled","pepers")
seq_along(pp)
for (i in seq_along(pp)) print(pp[i])
length(1:5)#长度
x<-c(1,2,3,5)
length(x)
length(x)<-3 #增加用缺失值补充
#索引向量
y<-(1:5)^2
y[c(1,3,5)]
y[c(-2,-4)]
y[c(F,T,T,F,T)]
#向量循环
1:5+1
1+1:5
1:5+1:15#较短加较长
rep(1:5,3)
rep(1:5,each=3)
rep_len(1:5,13)
#矩阵和数组
a_matrix<-matrix(1:12,nrow = 4,dimnames = list(c("one","two","three","four"),#行
c("ein","zwei","drei")#列
))
a_matrix
#数组
two_d_array<-array(1:12,dim = c(4,3),
dimnames = list(
c("one","two","three","four"),
c("ein","zwei","drei")
))
#列和维度
dim(two_d_array) #返回维度四行三列
dim(a_matrix)
#所有维度的乘积
length(two_d_array)
length(a_matrix)
rownames(a_matrix)#行名
colnames(a_matrix)#列名
#索引数组
a_matrix[1,c("zwei","drei")]#返回第一行第二列和第三列的值
a_matrix[1,]
a_matrix[2,]
....
#数组算术
another_matrix<-matrix(seq.int(2,24,2),
nrow = 4,
dimnames = list(
c("five","six","seven","eight"),
c("vier","funf","sechs")))
a_matrix+another_matrix
#####流程控制和循环!
if(T)message("it was true!")
if(F)message("it wasn't true!")
#执行多条语句,大括号
x<-3
if(x>2)#如果x>2,则执行下面的语句
{y<-2*x
z<-3*y}
#与if相对的是else,如果if的值为false,则执行else后面的代码
if(F){message("this won't execute...")}else{message("but this will")}
#else必须与if的右大括号在同一行,如果你把它挪到下一行将报错
#矢量化的if if(向量)
if(c(T,F))message("two choice"):the condition has length > 1#会报错
ifelse(condition,a,b)#为真返回a,为假返回b
yn<-rep.int(c(T,F),6)
ifelse(yn,1:3,-1:-12)
#多分支
#switch 第一个参数返回字符串的表达式,
#后面的参数与第一个参数相匹配时,返回语句块的值。匹配的参数必须与第一个参数完全相同
if(c(0))message("hello")
greek<-switch ("gama",
alpha=1,
beta=sqrt(4),
gama={
a<-sin(pi/3)
4*a^2
}
)#都不匹配返回null
greek
#####重复循环(repeat while for)
repeat{message("Happy Groundhog day!")
action<-sample(c(
"learn French",
"make an ice statue",
"Rob a bank",
"win heart of Andie Mcdowell"
),1)
message("action=",action)
if(action=="win heart of Andie Mcdowell")
break}
action<-sample(c(
"learn French",
"make an ice statue",
"Rob a bank",
"win heart of Andie Mcdowell"
),1)
while (action!="win heart of Andie Mcdowell") {
message("Happy groundhog day!")
action<-sample(c(
"learn French",
"make an ice statue",
"Rob a bank",
"win heart of Andie Mcdowell"
),1)
message("action=",action)}
#####for循环 已知代码执行次数的情形 接受一个迭代变量和一个向量参数,每次循环时,迭代变量从向量中取得一个值
for (i in 1:5)message("i=",i)
#执行多条语句用大括号
for (i in 1:5) {
j<-i^2
message("j=",j)}
#灵活 可以传字符向量逻辑向量或列表
for (month in month.name) {
message("The month of",month)
}
for (yn in c(T,F,NA)) {
message("This statement is",yn)
}
l<-list(pi,LETTERS[1:5],charToRaw("not as complicated as it looks"))
for (i in l) {
print(i)
}
#环境和函数
an_env<-new.env()
an_env[["pythag"]]<-c(12,15,20,21)
an_env$root<-polyroot(c(6,-5,1))
assign("monday",weekdays(as.Date("1969-07-20")),an_env)
exists("pythag",an_env)
nested_env<-new.env(parent = an_env)#父环境是an_env
exists("pythag",nested_env)
exists("pythag",nested_env,inherits = FALSE)


雷达卡


京公网安备 11010802022788号







