R语言实战(第2版)——第2章-2.2数据结构
#R语言实战
#第2章 创建数据集
#2.2 数据结构
#P21 标量:只含一个元素的向量,用于保存常量
f <- -3
g <- "US"
h <- TRUE
#P21 向量:用于存储数值型、字符型或逻辑型数据的一维数组。单个向量中的数据必须拥有相同的模式
a <- c(1,2,5,3,6,-2,4) #数值型向量
b <- c("one","two","three") #字符型向量
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE) #逻辑型向量
a <- c("k", "j", "h", "a", "c", "m")
a[3] #方括号返回给定元素所处位置的数值
a[c(1,3,5)]
a[2:6] #冒号用于生成一个数值序列
a <- c(2:6)
a <- c(2,3,4,5,6) #二者等价
#矩阵:二维数组,每个元素有相同的模式(数值型、字符型或逻辑型)
#matrix创建矩阵,ncol和nrow指定行和列的维度,dimnames行名、列名,byrow=T按行填充,byrow=F按列填充,默认按列填充
#mymatrix <- matrix(vector,nrow=numble_of_rows, ncol = number_of_columns,byrow = logical_value,
# dimnames = list(char_vector_rownames, char_vector_colnames))
#P22 2-1创建矩阵
y <- matrix(1:20,nrow = 5,ncol = 4)
cells <- c(1,26,24,68)
rnames <- c("R1","R2")
cnames <- c("C1","C2")
mymatrix <- matrix(cells,nrow = 2,ncol = 2,byrow = TRUE,dimnames = list(rnames,cnames)) #按行填充
mymatrix <- matrix(cells,nrow = 2,ncol = 2,byrow = FALSE,dimnames = list(rnames,cnames)) #按列填充
#使用下标和方括号选择矩阵的行列和元素
x <- matrix(1:10,nrow = 2)
x[2,]
x[,2]
x[1,4]
x[1,c(4,5)]
#数组:当维度超过2时,可以用数组代替矩阵
#P23 2-3创建数组
#myarray <- array(vector,dimensions,dimnames) #vector包含了数组中的数据,dimensions是数值型向量,给出了各维度下标的最大值,dimnames是可选的,各维度名称标签的列表
dim1 <- c("A1","A2")
dim2 <- c("B1","B2","B3")
dim3 <- c("C1","C2","C3","C4")
z <- array(1:24,c(2,3,4),dimnames = list(dim1,dim2,dim3))
#使用方括号和下标选择数组中的元素
z[1,2,3]
#数据框:多种数据模式,包含数值型、字符型、逻辑型
#mydata <- data.frame(col1,col2,col3,...)
#P24 2-4创建数据框
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabates <- c("type1","type2","type1","type1")
status <- c("poor","improved","excellent","poor")
patientdata <- data.frame(patientID,age,diabates,status)
#P24 2-5选取数据框中的元素,下标和列名等价,美元符$列名
patientdata[1:2]
patientdata[c("diabates","status")]
patientdata$age
table(patientdata$diabates,patientdata$status) #生成列联表
#在每个变量名前都输一边数据框名$太麻烦了,走一些捷径:attach()/detach()/with()
summary(mtcars$mpg)
plot(mtcars$mpg,mtcars$disp)
plot(mtcars$mpg,mtcars$wt)
#也可写成
attach(mtcars) #将数据框添加到R的搜索路径中
summary(mpg)
plot(mpg,disp)
plot(mpg,wt)
detach(mtcars) #将数据框从搜索路径中移除
#也可写成
with(mtcars,{
print(summary(mpg))
plot(mpg,disp)
plot(mpg,wt)
})
#with赋值仅在括号内生效,若需创建在括号外生效的变量,是用特殊赋值符号<<-
with(mtcars,{
nokeepstats <- summary(mpg)
keepstats<<-summary(mpg)
})
nokeepstats
keepstats
#实例标识符
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabates <- c("type1","type2","type1","type1")
status <- c("poor","improved","excellent","poor")
patientdata <- data.frame(patientID,age,diabates,status,row.names = patientID) #指定patientID作为打印输出和图形中实例名称所用变量
#因子:名义和有序变量在R中称为因子
diabates <- c("type1","type2","type1","type1")
diabates <- factor(diabates)
status <- c("poor","improved","excellent","poor")
status <- factor(status,ordered = T) #1=excellent 2=improved 3=poor
status <- factor(status,order=T,levels = c("poor","improved","excellent")) #指定levels覆盖默认顺序
sex <- factor(sex,levels = c(1,2),labels = c("male","female")) #数值型变量编码成因子,所有非1非2均被当做缺失值
#P28 2-6因子的使用
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabates <- c("type1","type2","type1","type1")
status <- c("poor","improved","excellent","poor")
diabates <- factor(diabates)
status <- factor(status,ordered = T)
patientdata <- data.frame(patientID,age,diabates,status)
str(patientdata) #显示数据框的信息
summary(patientdata) #区别对待不同类型变量
#列表:R中最复杂的数据类型,是一些对象的有序集合。
作用:R中的很多结果都是以list返回的,主要是访问。
#mylist <- list(object1,object2,...)
#mylist <- list(name1=object1,name2=object2,...)
#P29 2-7创建一个列表
g <- "my first list"
h <- c(25,26,18,39)
j <- matrix(1:10,nrow = 5)
k <- c("one","two","three")
mylist <- list(title=g,ages=h,j,k) #列表含四个成分:字符串、数值型向量、矩阵、字符型向量
mylist
mylist[[2]] #双重括号指明某个成分的数字或名称来访问列表中的元素
mylist[["ages"]]