5. 数据结构
5.1 数据结构简介
(1)向量
一个向量的所有元素必须有相同的类型(模式)
(2)列表
列表可以非同质的
列表可按位置索引:lst[[2]]
抽取子列表:lst[c(2,5)]
列表可以有名称:lst[[“Moe”]]或者lst$Moe
列表类似于字典、散列表等
(3)模式:实体类型
> mode(3.1415)
R中每个对象都有一个模式,表明该对象如何存储在存储器中:
对象 |
例子 |
模式 |
Number |
3.14 |
numeric |
Vector of numbers |
c(2.7, 3.14) |
numeric |
Character string |
“Moe” |
character |
Vector of Character string |
c(“Moe”, “Larry”) |
character |
Factor |
factor(c(“NY”,”CA”,”IL”)) |
numeric |
List |
list(“Moe”,”Larry”) |
list |
Data frame |
data.frame(x=1:3, y=c(“NY”,”CA”,”IL”)) |
list |
Function |
|
function |
(4)类:抽象类型
> d <—as.Date(“2010-03-10”)
&g