04.数据结构Ⅱ—数据框,因子,列表
四、数据框(数据表)
R语言中做统计分析的样本数据,都是按数据框类型操作的。
数据框的每一列代表一个变量属性的所有取值,每一行代表一条样本数据。
1.创建数据框
通过函数data.frame()把多个向量组合起来创建,并设置列名称。其基本格式为:
data.frame(col1,col2,col3,...)
其中,列向量col1,col2,col3,…可以为任意类型。
注:矩阵也可以通过函数data.frame()转化为数据库。
data_iris-data.frame(Sepal.Length=c(5.1,4.9,4.7,4.6),Sepal.Width=c(3.5,3.0,3.2,3.1),Petal.Length=c(1.4,1.4,1.3,1.5),Petal.Width=rep(0.2,4))
data_iris
Sepal.LengthSepal.WidthPetal.LengthPetal.Width
15.13.51.40.2
24.93.01.40.2
34.73.21.30.2
44.63.11.50.2
#矩阵转化为数据框
dmatrix-matrix(1:8,c(4,2))
dmatrix
[,1][,2]
[1,]15
[2,]26
[3,]37
[4,]48
data.frame(dmatrix)
X1X2
2.数据框索引
列标或列名称索引:
data_iris[,1]——返回数据框data_iris的第1列
data_iris$Sepal.Length或data_iris["Sepal.Length"]
——同data_iris[,1]
行索引:
data_iris[1,]——返回数据框data_iris的第1行
data_iris[1:3,]——返回数据框data_iris的第1至3行
元素索引:
data_iris[1,1]——返回数据框data_iris的第1列第1个数据
data_iris$Sepal.Length[1]或data_iris["Sepal.Length"][1]——返回数据框data_iris的Sepal.Length列第1个数据
用函数subset()按条件索引
subset(data_iris,Sepal.Length5)
Sepal.LengthSepal.WidthPetal.LengthPetal.Width
24.93.01.40.2
34.73.21.30.2
44.63.11.50.2
注:还可用sqldf包中的sqldf()函数,借助sql语句索引。例如,
library(sqldf)
sqldf("select*frommtcarswherecarb=1orderbympg",row.names=TRUE)
3.数据框的编辑
类似矩阵操作,可通过函数rbind(),增加行(样本数据),要求宽度(列数)相同;函数cbind(),增加列(属性变量),要求高度(行数)相同。
删除样本(行),类似矩阵操作。
用函数names()查看或修改数据框的列名。
#增加样本数据(行)
data_iris-rbind(data_iris,list(5.0,3.6,1.4,0.2))
data_iris
Sepal.LengthSepal.WidthPetal.LengthPetal.Width
15.13.51.40.2
24.93.01.40.2
34.73.21.30.2
44.63.11.50.2
55.03.61.40.2
#增加属性变量(列)
data_iris-cbind(data_iris,Species=rep("setosa",5))
data_iris
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
15.13.51.40.2setosa
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
#删除数据
data_iris[,-1]#删除第1列
Sepal.WidthPetal.LengthPetal.WidthSpecies
13.51.40.2setosa
23.01.40.2setosa
33.21.30.2setosa
43.11.50.2setosa
53.61.40.2setosa
data_iris[-1,]#删除第1行
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
24.93.01.40.2setosa
34.73.21.30.2setosa
44.63.11.50.2setosa
55.03.61.40.2setosa
#编辑数据框列名
names(data_iris)#查看数据框列名
[1]"Sepal.Length""Sepal.Width""Petal.Length""Petal.Width""Species"
names(data_iris)[1]="sepal.length"#修改第1列列名
names(data_iris)
[1]"sepal.length""Sepal.Width""Petal.Length""Petal.Width""Species"
五、因子(factor)
变量分为名义型(无顺序好坏之分的分类变量,如性别)、有序型(有顺序好坏之分的分类变量,如疗效)、连续型(通常的数值变量,可带小数位)。
名义型和有序型的类别变量,在R中称为因子。
因子提供了一个简单且紧凑的形式来处理分类数据,因子用水平来表示所有可能的取值,例如,性别有两个水平:男、女。
1.创建因子
(1)用函数factor(),基本格式为:
factor(x,levels,labels=...,exclude=...,
ordered=...,nmax=...)
其中,x为创建因子的数据向量;levels指定因子的水平数,默认为x中不重复的所有值;labels设置各水平名称(前缀),与水平一一对应;exclude指定有哪些水平是不需要的;ordered设置是否对因子水平排序,默认为TRUE即有序因子,FALSE为无序因子;nmax设定水平数的上限。
ff-factor(substring("statistics",1:10,1:10),levels=letters)
ff
[1]statistics
Levels:abcdefghijklmnopqrstuvwxyz
ff[,drop=TRUE]#去掉未包含在向量中的水平,同f.-factor(ff)
[1]statistics
Levels:acist
factor(1:10,labels="let")
[1]let1let2let3let4let5let6let7let8let9let10
Levels:let1let2let3let4let5let6let7let8let9let10
factor(LETTERS[3:1],ordered=TRUE)
[1]CBA
Levels:ABC
注:函数substring()用来提取字符串的子串,第2个参数是起始位置,第3个参数是终止位置;letters和LETTERS是R中专有变量,表示26个小写/大写字母组成的字符向量。
(2)用函数gl()创建因子序列
用函数gl()生成不同水平的因子序列,基本格式为:
gl(n,k,length=n*k,labels=seq_len(n),ordered=FALSE)
其中,n表示因子水平数;k表示每个水平的重复数;length表示生成序列的长度;labels为表示因子水平的n维向量;ordered指定是否为有序因子,TRUE为有序因子,FALSE为无序因子。
#生成水平数为3,每个水平重复2次的因子序列
gl(3,2)
[1]
Levels:
#生成水平为“TRUE”和“FALSE”,每个水平重复3次的因子序列
gl(2,3,labels=c("TRUE","FALSE"))
[1]TRUETRUETRUEFALSEFALSEFALSE
Levels:TRUEFALSE
#生成水平数为2,序列长度为10的因子序列
gl(2,1,10)
[1]
Levels:12
#生成水平数为3,每个水平重复2次的有序因子序列
gl(3,2,ordered=TRUE)
[1]
Levels:
2.因子的存储方式
R语言中,因子是以整数型向量存储的,每个因子水平对应一个整数型的数。对字符型向量创建的因子,会按照字母顺序排序,再对应到整数型向量。
status-c("Poor","Improved","Excellent","Poor")
class(status)
[1]"character"
status.factor-factor(status,ordered=TRUE)
status.factor
[1]PoorImprovedExcellentPoor
Levels:ExcellentImprovedPoor
class(status.factor)
[1]"ordered""factor"
storage.mode(status.factor)
[1]"integer"
as.numeric(status.factor)
[1]
levels(status.factor)
[1]"Excellent""Improved""Poor"
六、列表
列表就是一些对象或成分的有序集合(组合方式更自由)。
列表允许整合若干对象到单个对象名下,例如,某个列表可能是若干向量、矩阵、数据框,甚至是其它列表的组合。
一般在使用R语言进行数据分析和挖掘的过程中,向量和数据框是用的最多的,列表常在存储较复杂的数据时作为数据对象类型。
列表提供了一种简单的方式来组织和重新调用不相干的信息。另外,许多R函数的运行结果都是用列表形式返回的。
1.创建列表
用函数list(),基本格式为:
list(object1,object2,...)
其中,object对象可以是任何类型。
若同时为列表中的对象命名:
list(name1=object1,name2=object2,...)
data-list(a=c(1,2,3,4),b=c("one","two","three"),c=c(TRUE,FALSE),d=(1+2i))
data
$a
[1]4
$b
[1]"one""two""three"
$c
[1]TRUEFALSE
$d
[1]1+2i
#查看列表的数据结构
summary(data)
LengthClassMode
a4-none-numeric
b3-none-character
c2-none-logical
d1-none-白癜风的发病原因有哪些出名的白癜风医院