“做好准备,用R创建出高品质的程序,迅速提高你的水平吧! ” ——Patrick Breen,罗杰斯通信公司


       任何数据分析的第一步,是按照需求的格式来创建含有研究信息的数据集,本节描述了向量、矩阵、数组、数据框以及列表的用法。熟悉这些数据结构以及访问其中元素的表述方法,十分有助于了解R的工作方式,但是也需要耐心来慢慢消化这一节的内容。

1.向量

代 码

解 释

x<-c(1,2,3,4)

创建数值型向量x,元素为1,2,3,4

y<-c(“one”,“two”,“ttree”)

创建字符型向量y,元素为one,two,three

z<-c(TRUE,FALSE,TRUE)

创建逻辑性向量z,元素为TRUE,FALSE,TRUE

a<-seq(1,10,2)

生成从1到10,步长为2的向量a

b<-seq(10,1,-1)

生成从10到1,步长为-1的向量b

c<-seq(1,by=2,length=10)

生成从1开始,步长为2,长度为10的向量c

d<-rep(c(1,2),2)

重复元素1和2,结果为1,2,1,2

e<-rep(c(1,2),each=2)

每个元素重复2次,结果为1,1,2,2

x[2]

提取向量x的第二个元素,结果为2

x[c(2,4)]

提取向量x的第二个和第四个元素,结果为2,4

x[2:4]

提取向量x第二个至第四个的元素

x[x>2]

提取向量x中大于2的元素

x[-2]

删除向量x的第二个元素

x[2]<-5

将向量x的第二个元素赋值为5

2.矩阵

代 码

解 释

matrix1<matrix(1:20,nrow=5,ncol=4,byrow=TRUE,

dimnames=list(c(“R1”,“R2”,“R3”,“R4”,“R5”),

c(“Q1”,“Q2”,“Q3”,“Q4”)))

生成5行4列的矩阵,行名依次为R1至R5,

列名依次为Q1至Q4,逐行填充

matrix1[3,]

提取矩阵第三行的元素

matrix1[,3]

提取矩阵第三列的元素

matrix1[1,3]

提取矩阵第一行第三列位置的元素

matrix1[1,c(2,3)]

提取第一行、第二第三列的元素

> matrix1<-matrix(1:20,nrow=5,ncol=4,
+ byrow=TRUE,dimnames=list(c("R1","R2",
+ "R3","R4","R5"),c("Q1","Q2","Q3","Q4")))
> matrix1
   Q1 Q2 Q3 Q4
R1  1  2  3  4
R2  5  6  7  8
R3  9 10 11 12
R4 13 14 15 16
R5 17 18 19 20
> matrix1[3,]
Q1 Q2 Q3 Q4 
 9 10 11 12 
> matrix1[,3]
R1 R2 R3 R4 R5 
 3  7 11 15 19 
> matrix1[1,3]
[1] 3
> matrix1[1,c(2,3)]
Q2 Q3 
 2  3

3.数组

代 码

解 释

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))

由 dim1,dim2,dim3 生成三维数组 z,各维度标签名称为向量自身名称

> 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
, , C1
   B1 B2 B3
A1  1  3  5
A2  2  4  6
, , C2
   B1 B2 B3
A1  7  9 11
A2  8 10 12
, , C3
   B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
   B1 B2 B3
A1 19 21 23
A2 20 22 24

4.数据框

代 码

解 释

patientID <- c(1, 2, 3, 4)

age <- c(25, 34, 28, 52)

diabetes <- c(“Type1”, “Type2”, “Type1”, “Type1”)

status <- c(“Poor”, “Improved”, “Excellent”, “Poor”)

patientdata <-data.frame(patientID, age, diabetes, status)

由 patientID,age,diabetes,status 生成数据框 patientdata

patientdata[1:2]

提取patientdata的第一、第二列

patientdata[c(“diabetes”, “status”)]

提取patientdata的diabetes和status两个变量

table(patientdata$diabetes,patientdata$status)

生成patientdata中diabetes和status变量的列联表

new<-patientdata[which(patientdata$age>30)]

提取patientdata中年龄大于30的观测值,并保存在new中

str(diabetes)

显示变量diabetes的结构

str(patientdata)

显示数据框patientdata的行列结构、变量组成、变量类型

> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1"," Type2", "Type1", "Type1")
> status <- c("Poor", "Improved"," Excellent"," Poor")
> patientdata <-data.frame(patientID, age, diabetes, status)
> patientdata[1:2]
  patientID age
1         1  25
2         2  34
3         3  28
4         4  52
> patientdata[c("diabetes", "status")]
  diabetes     status
1    Type1       Poor
2    Type2   Improved
3    Type1  Excellent
4    Type1       Poor
>table(patientdata$diabetes,patientdata$diabetes,patientdata$status)
, ,  =  Excellent     
          Type2 Type1
   Type2      0     0
  Type1       0     1
, ,  =  Poor      
          Type2 Type1
   Type2      0     0
  Type1       0     1
, ,  = Improved     
          Type2 Type1
   Type2      1     0
  Type1       0     0
, ,  = Poor  
          Type2 Type1
   Type2      0     0
  Type1       0     1
> new<-patientdata[which(patientdata$age>30)]
> new
  age     status
1  25       Poor
2  34   Improved
3  28  Excellent
4  52       Poor

5.因子

status<-factor(status,order=TRUE,levels=c(“Poor”,“Iproved”,“Excellent”))
status<-c(“Poor”,“Iproves”,“Excellent”,“Poor”)
status<-factor(status,order=TRUE)
diabetes<-c(“Type1”,“Type2”,“Type1”,“Type1”)
diabetes<-factor(diabetes)
diabetes<-c(“Type1”,“Type2”,“Type1”,“Type1”)
diabetes<-factor(diabetes)
status<-c(“Poor”,“Iproves”,“Excellent”,“Poor”)
status<-factor(status,order=TRUE)
diabetes<-c(“Type1”,“Type2”,“Type1”,“Type1”)
diabetes<-factor(diabetes)
status<-factor(status,order=TRUE,levels=c(“Poor”,“Iproved”,“Excellent”))
status<-c(“Poor”,“Iproves”,“Excellent”,“Poor”)
status<-factor(status,order=TRUE)
diabetes<-c(“Type1”,“Type2”,“Type1”,“Type1”)
diabetes<-factor(diabetes)

6. 列表

代码

解释

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,将 g 命名为 title,将 h 命名为 ages

mylist[[2]]

提取列表mylist中的第二个分量

mylist[[“ages”]]
或者
mylist$ages

提取mylist中的ages分量

length(mylist)

显示列表mylist的分量数

names(mylist)

显示mylist的分量名称

提取列表mylist第二个分量的第一至第三个值
mylist[[2]][1:3]
mylist[[2]][1:3]
提取列表mylist第二个分量的第一至第三个值
mylist[[2]][1:3]