#earray<-array(thing="hat",size=3.2,lenth=2)# 不可以类型要相同
earray<-array(c(1,2,3,4,5,6,7),dim=c(1,2,3,4))
earray
## , , 1, 1
## 
##      [,1] [,2]
## [1,]    1    2
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]    3    4
## 
## , , 3, 1
## 
##      [,1] [,2]
## [1,]    5    6
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]    7    1
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]    2    3
## 
## , , 3, 2
## 
##      [,1] [,2]
## [1,]    4    5
## 
## , , 1, 3
## 
##      [,1] [,2]
## [1,]    6    7
## 
## , , 2, 3
## 
##      [,1] [,2]
## [1,]    1    2
## 
## , , 3, 3
## 
##      [,1] [,2]
## [1,]    3    4
## 
## , , 1, 4
## 
##      [,1] [,2]
## [1,]    5    6
## 
## , , 2, 4
## 
##      [,1] [,2]
## [1,]    7    1
## 
## , , 3, 4
## 
##      [,1] [,2]
## [1,]    2    3
# 提取所有第三维度为3,第四维度为2的所有元素
earray[,,3,2]
## [1] 4 5

数组array(多维向量)

一维向量、二维矩阵、三维数组、更多维数组;

二维矩阵是数组能表示的最大单元

维度与维度之间用“ ,”表示

某个维度的全部元素

elist<-list(thing ="hat,building",thing="多了一个",size=c(3.2,1600),120,length=2,2000)
eframe<-data.frame(thing ="hat",size=3.2,length=2)
elist1<-list(thing ="people",length=180,width=50,"我没有名字")
elist2<-list(K="我是字符向量")
elist.list<-list(smalllist1=elist,smalllist2=elist1,smalllist3=elist2) #嵌套列表名
elist.list
## $smalllist1
## $smalllist1$thing
## [1] "hat,building"
## 
## $smalllist1$thing
## [1] "多了一个"
## 
## $smalllist1$size
## [1]    3.2 1600.0
## 
## $smalllist1[[4]]
## [1] 120
## 
## $smalllist1$length
## [1] 2
## 
## $smalllist1[[6]]
## [1] 2000
## 
## 
## $smalllist2
## $smalllist2$thing
## [1] "people"
## 
## $smalllist2$length
## [1] 180
## 
## $smalllist2$width
## [1] 50
## 
## $smalllist2[[4]]
## [1] "我没有名字"
## 
## 
## $smalllist3
## $smalllist3$K
## [1] "我是字符向量"

列表

列表中的元素可以是不同元素,列表为一条一条的堆积

列表中可以包含列表

列表索引表示方法:

[[2]] # 该数据表中第几个列表,当小列表有名字时,[[2]]为小列表名称**\(列表名**(\)smalllist2)

[[2]]$width # 列表2中,名称为width的元素,索引

[1] 50 # 元素 value

[[2]][[4]] # 元素没有名称的时候用元素在列表中的排序序号表示,这里为elist.list的第四个元素 [1] “我没有名字”

数据框

数据框是特殊的列表 要求每一类的数据类型相同

对象和类

对象就是一个实例

对象属于类

list函数属于 function类

class定义一个对象的类

class(elist.list)
## [1] "list"
class(list)  #类为function
## [1] "function"
class(typeof) #类为function
## [1] "function"

R语法

1、R为解释型语言。

2、所有R程序是由一组表达式构成的

3、R中一切为对象:向量、列表、函数

4、函数:是一种特殊的对象输入若干个对象,输出一个对象

5、符号:函数名,变量名符号

6、表达式:赋值语句、条件语句、表达式–>所有语句都可以写成函数形式

R解释器

1、R解释器解释每一个表达式,将语法糖转换为函数形式,根据情况符号 转换为对象,执行每一个表达式并返回一个对象

R是怎么工作的?

x<-1
if (x>1) "orange" else "apple"
## [1] "apple"
quote(if (x>1) "orange" else "apple")
## if (x > 1) "orange" else "apple"
typeof(quote(if (x>1) "orange" else "apple")) # quote解析语法,返回的信息有限
## [1] "language"
as(quote(if (x>1) "orange" else "apple"),"list") # 将language对象转换成list对象
## [[1]]
## `if`
## 
## [[2]]
## x > 1
## 
## [[3]]
## [1] "orange"
## 
## [[4]]
## [1] "apple"
lapply(as(quote(if (x>1) "orange" else "apple"),"list"), typeof) # lapply为list中每一个元素typeof操作,`if`是符号,是函数名 
## [[1]]
## [1] "symbol"
## 
## [[2]]
## [1] "language"
## 
## [[3]]
## [1] "character"
## 
## [[4]]
## [1] "character"

1、解释器将表达式解析为函数,包括函数对象符号,输入对象符号

2、quote函数——解析表达式,表达式被解析为language对象,更详细内容,可以用as.list()函数转化为list,利用lapply(typeof)查看每一个对象的类型。实际上第一个都是符号,即函数名,

3、注意“else”不包括在解析结果中!?

小结

面向对象的编程语言:掌握基本的关系,理解基本名词。

1、R语言中的 向量、比向量复杂的数据结构(数组、列表)、函数、模型、图形都是对象!

2、对象都是某个类的成员(与特定类相关联的函数是方法、作用于不同类具有相同名称的方法称为泛型函数)!

3、获得帮助:example(),??

4、R工作机制:表达式被解释器解释为 函数对象 和 输入对象,函数名、变量名是符号。