Open JackieMium opened 6 years ago
内容来自于看《R 语言实战》时做的笔记 2017-04-24
向量是一个一维数组,用于存储数值型、字符型或逻辑型数据。执行组合功能的函数c()可用来创建向量;
c()
> a<-c(1, 2, 5, 3, 6, 2, 4) > b<-c("one", "two", "three") > c<-c(TRUE,TRUE,TRUE,FALSE,TURE,FALSE)
a 是数值型向量,b 是字符型向量,c 是逻辑向量。
> a <- c(1, 2, 3, 4, 5, 6) > a[3] [1] 3 > a[c(1, 3, 5)] [1] 1 3 5 > a[2:6] [1] 2 3 4 5 6
mymatrix <- matrix(vector,nrow=number_of_rows,ncol=number_of_columns, byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))
vector
nrow
ncol
dimnames
byrow
byrow=TRUE
(byrow=FALSE)
x[i,]
x[,j]
x[i,j]
> cells <- c(1, 2, 3, 4) > rnames <- c("R1","R2") > cnames <- c("C1","C2") > mymatrix <- (cells,nrow=2,ncol=2,byrow=TRUE,dimnames=list(rnames,cnames) > mymatrix C1 C2 R1 1 2 R2 3 4 > ymatrix <- matrix(1:20,nrow=4,ncol=5) > y [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > y[,1] [1] 1 2 3 4 > y[2,2] [1] 6 > y[1,c(4,5) [1] 13 17
myarray <- array(vector, dimensions, dimnames)
dimensions
z<-array(1:24,c(2,3,4), dimnames=list(dim1,dim2,dim3))
c(2,3,4)
data.frame()
mydata <- data.frame(col1, col2, col3,...)
col1
col2
col3
names
> patientID <- c(1,2,3,4) > age <- c(23,24,25,26) > diabetes <- c("Type1","Type2","Type1","Type2") > status <- c("Poor","Improved","Excellent","Poor") > patientdata <- data.frame(patientID, age, diabetes,status) > patientdata patientID age diabetes status 1 1 23 Type1 Poor 2 2 24 Type2 Improved 3 3 25 Type1 Excellent 4 4 26 Type2 Poor
> patientdata[1,2] patientID age 1 1 23 2 2 24 3 3 25 4 4 26 > patientdata[,3:4] diabetes status 1 Type1 Poor 2 Type2 Improved 3 Type1 Excellent 4 Type2 Poor > patientdata[c("diabetes","status") diabetes status 1 Type1 Poor 2 Type2 Improved 3 Type1 Excellent 4 Type2 Poor > patientdata$age [1] 23 24 25 26 > patienttable <- table(patientdata$diabetes,patientdata$status) > patienttable Excellent Improved Poor Type1 1 0 1 Type2 0 1 1
patientID
row.names
> patientdata <- data.frame(patientID, age, diabetes, status, row.names = patientID) > patientdata patientID age diabetes status 1 1 23 Type1 Poor 2 2 24 Type2 Improved 3 3 25 Type1 Excellent 4 4 26 Type2 Poor > patientdata <- data.frame(patientID, age, diabetes, status, row.names = age) > patientdata patientID age diabetes status 23 1 23 Type1 Poor 24 2 24 Type2 Improved 25 3 25 Type1 Excellent 26 4 26 Type2 Poor
Diabetes(Types1,Type2)
Status(poor,improved,excellent)
poor
improved
Age
factor
factor()
diabetes <- c("Type1", "Type2", "Type1", "Type1")
diabetes <- factor(diabetes)
(1,2,1,1)
1=Type1
2=Type2
diabetes
ordered=TRUE
status <- c("Poor", "Improved", "Excellent", "Poor")
status <- factor(status, ordered=TRUE)
(3,2,1,3)
1=Excellent
2=Improved
3=Poor
status <- factor(status, order=TRUE, leves=c("Poor","Improved","Excellent"))
1=Poor
3=Excellent
> diabetes <- factor(diabetes) > diabetes [1] Type1 Type2 Type1 Type2 Levels: Type1 Type2 > str(patientdata) 'data.frame': 4 obs. of 4 variables: $ patientID: num 1 2 3 4 $ age : num 23 24 25 26 $ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 2 $ status : Factor w/ 3 levels "Excellent","Improved",..: 3 2 1 3 > status <- factor(status,ordered = TRUE) > status [1] Poor Improved Excellent Poor Levels: Excellent < Improved < Poor > str(patientdata) 'data.frame': 4 obs. of 4 variables: $ patientID: num 1 2 3 4 $ age : num 23 24 25 26 $ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 2 $ status : Factor w/ 3 levels "Excellent","Improved",..: 3 2 1 3 > status <- factor(status,ordered = TRUE,levels = c("Poor","Improved","Excellent")) > status [1] Poor Improved Excellent Poor Levels: Poor < Improved < Excellent > str(patientdata) 'data.frame': 4 obs. of 4 variables: $ patientID: num 1 2 3 4 $ age : num 23 24 25 26 $ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 2 $ status : Factor w/ 3 levels "Excellent","Improved",..: 3 2 1 3 > summary(patientdata) patientID age diabetes status Min. :1.00 Min. :23.00 Type1:2 Excellent:1 1st Qu.:1.75 1st Qu.:23.75 Type2:2 Improved :1 Median :2.50 Median :24.50 Poor :2 Mean :2.50 Mean :24.50 3rd Qu.:3.25 3rd Qu.:25.25 Max. :4.00 Max. :26.00
list()
mylist <- list(object1,object2,...)
mylist <-list(name1=object1, name2=object2, ...)
mylist[[2]]
mylist[["name2"]]
向量
向量是一个一维数组,用于存储数值型、字符型或逻辑型数据。执行组合功能的函数
c()
可用来创建向量;a 是数值型向量,b 是字符型向量,c 是逻辑向量。
矩阵
mymatrix <- matrix(vector,nrow=number_of_rows,ncol=number_of_columns, byrow=logical_value, dimnames=list(char_vector_rownames, char_vector_colnames))
其中vector
包含了矩阵的元素,nrow
和ncol
用以指定行和列的维数,dimnames
包含了可选的、以字符型向量表示的行名和列名。选项byrow
则表明矩阵应当按行填充(byrow=TRUE
)还是按列填充(byrow=FALSE)
,默认情况下按列填充x[i,]
指矩阵X中的第i行,x[,j]
指矩阵X中的第j列,x[i,j]
指第i行第j个元素。选择多行或多列时,下标i和j可为数值型向量数组
myarray <- array(vector, dimensions, dimnames)
。 其中vector
包含了数组中的数据,dimensions
是一个数值型向量,给出了各个维度下标的最大值,而dimnames
是可选的、各维度名称标签的列表。z<-array(1:24,c(2,3,4), dimnames=list(dim1,dim2,dim3))
,c(2,3,4)
表示二行三列四组 数组与矩阵一样,只能拥有一种模式。数据框
data.frame()
创建,mydata <- data.frame(col1, col2, col3,...)
其中的列向量col1
,col2
,col3
...可为任何类型(如字符型、数值型或逻辑型), 每一列的名称可由函数names
指定。每一列数据的模式必须唯一,不过可以将多个模式的不同列放到一起组成数据框。patientID
)用于区分数据集中不同的个体。在R中,实例标识符(case identifier)可通过数据框操作函数中的row.names
选项指定。因子
Diabetes(Types1,Type2)
是名义型变量的一例;Status(poor,improved,excellent)
是顺序型变量的一个佳例,病情为poor
的病人状态不如improved
的病人,但并不知道相差多少;Age
就是一个连续型变量。factor
)。因子在R中非常重要,因为它决定了数据的分析方式以及如何进行视觉呈现。factor()
以一个整数向量的形式存储类别值,整数的取值范围1....k(其中k是名义型变量中唯一值的个数),同时一个由字符串(原始值)组成的内部向量将映射到这些整数上。diabetes <- c("Type1", "Type2", "Type1", "Type1")
语句diabetes <- factor(diabetes)
将此向量储存为(1,2,1,1)
,并在内部将其关联为1=Type1
和2=Type2
(具体赋值根据字母顺序而定)。针对向量diabetes
进行的任何分析都会将其作为名义型变量,并自动选择合适的统计方法;factor()
指定参数ordered=TRUE
。给定向量status <- c("Poor", "Improved", "Excellent", "Poor")
,语句status <- factor(status, ordered=TRUE)
会将向量编码为(3,2,1,3)
,并在内部将这些值关联为1=Excellent
,2=Improved
,以及3=Poor
。另外,针对此向量进行的任何分析都会将其作为有序型变量对待,并自动选择合适的统计方法;对于字符型变量,因子的水平默认依字母的顺序创建,但按默认的字母顺序排序的因子很少能够让人满意。可以通过指定levels选项来覆盖默认排序,status <- factor(status, order=TRUE, leves=c("Poor","Improved","Excellent"))
各水平的赋值将为1=Poor
,2=Improved
,3=Excellent
。请保证指定的水平与数据中真实值相匹配,因为任何在数据中出现而未在参数中列举的数据都将被设为缺失值。列表
list()
创建列表。mylist <- list(object1,object2,...)
;其中的对象可以是目前为止讲到的任何结构,还可以为列表中的对象命名:mylist <-list(name1=object1, name2=object2, ...)
mylist[[2]]
和mylist[["name2"]]
均指第二个元素。