# Understanding the Dataset
# 1.1 Vector
a=c(1,2,5,3,6,-2,4)
b=c("one","two","three")
c=c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE)
# 1.2 Matrix
x=matrix(1:20,nrow = 5,ncol = 4,byrow = TRUE) # 以行为顺序填充
x
y=matrix(1:20,nrow = 5,ncol = 4,byrow = FALSE) # 以列为顺序填充
y
x[2,] # 选择第2行的数据
x[,2] # 选择第2列的数据
x[1,4] # 选择第1行第4列的数据
x[2,c(2,4)] # 选择第2行,第2,4列的数据
x[3:5,2] #选择第3,4,5行,第2列的数据
rnames=c("apple","banana","orange","melon","corn")
cnames=c("cat","dog","bird","pig")
x=matrix(1:20,nrow = 5,ncol = 4,byrow = TRUE)
rownames(x)=rnames # 行名
colnames(x)=cnames # 列名
x
x[,"cat"] # 取cat一列
# 1.3 Array
dim1=c("A1","A2")
dim2=c("B1","B2","B3")
dim3=c("C1","C2","C3","C4")
dim4=c("D1","D2","D3")
z=array(1:72,c(2,3,4,3),dimnames = list(dim1,dim2,dim3,dim4)) # 创建了一个四维数组
z
z[1,2,3,]
# 1.4 Data Frame (可以视为大的矩阵)
patientID=c(1,2,3,4)
age=c(24,34,28,52)
diabetes=c("Type1","Type2","Type1","Type1")
status=c("Poor","Improved","Excellent","Poor")
patientdata=data.frame(patientID,age,diabetes,status)
patientdata
patientdata[1:2] # 取第1,2列的内容
patientdata[1,1:3] # 取第1行,第1,2,3列的内容
patientdata[c(1,3),1:3] # 取第1,3行,第1,2,3列的内容
# 1.5 List
mylist=list(patientdata,x)
mylist
mylist[[1]][1,] # 取第一个list也即patientdata的第1行