1 / 54

第二讲 R 语言的基本数据结构

第二讲 R 语言的基本数据结构. 作业 1: 按下面表格的形式生成本组信息的数据框 , 并提交原程序 .Name 为字符型、 ID 为数字型、 Team.Num 为分组因子、 Duty 为分组因子、 Grade1-5 为数值型. 作业 2 :利用 sample 函数设计算法,把下面 16 个 demo 公平地分配给 15 个组,并提交原程序. Base: is.things Base: recursion Graphics: Hershey Graphics: Japanese Graphics: graphics Graphics: image

addo
Download Presentation

第二讲 R 语言的基本数据结构

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第二讲 R语言的基本数据结构

  2. 作业1:按下面表格的形式生成本组信息的数据框,并提交原程序.Name为字符型、ID为数字型、Team.Num为分组因子、Duty为分组因子、Grade1-5为数值型作业1:按下面表格的形式生成本组信息的数据框,并提交原程序.Name为字符型、ID为数字型、Team.Num为分组因子、Duty为分组因子、Grade1-5为数值型

  3. 作业2:利用sample函数设计算法,把下面16个demo公平地分配给15个组,并提交原程序作业2:利用sample函数设计算法,把下面16个demo公平地分配给15个组,并提交原程序 Base: is.things Base: recursion Graphics: Hershey Graphics: Japanese Graphics: graphics Graphics: image Graphics: persp Graphics: plotmath Lattice: intervals Lattice: labels Lattice: lattice Lattice: panel Stats: glm.vr Stats: lm.glm Stats: nlm Stats: smooth

  4. 一、R语言的对象 • 所有在R语言中可操作的各种数据及各种表达式等都叫R语言的操作对象。 对象命名: 1、对象的名字必须是以一个字母开头(A–Z 或a–z),中间可以包含字母,数字(0–9),点(.)及下划线(_); 2、R对对象的名字区分大小写; 3、避免用R的各种包中的既有对象名来对对象命名:例如,if、for、pi等 3、可以通过输入一个对象的名字来显示其内容,例如,一个名为n的对象,其内容是数值10: > n [1] 10

  5. 对象的产生、查询及删除 1、赋值:<-或= 2、函数ls的功能是显示所有在内存中的对象:只会列出对象名 >name<-”Carmen”;n1<-10;n2<-100;m1<-1 >ls() >ls(pat=“m”) >ls.str() //将会展示内存中所有对象的详细信息

  6. 3、删除:>rm(x) 删除内存中所有对象:>rm(list=ls()) 注:ls()函数中的一些选项同样可以运用到 rm中来

  7. 对象的基本属性 • 每个对象包含很多基本属性,常用的有对象的类型、存储类型、对象模式、对象长度、对象维度、对象名称等。 对象的类型:>mode(object) 数值型Numeric 如 100, 0, -4.335 字符型Character 如“China” 逻辑型Logical 如TRUE, FALSE 因子型Factor 表示不同类别 复数型Complex 如:2 + 3i 对象的长度:>length(object); > fruit<-c(5,10,1,20) > names(fruit)<-c("orange","banana","apple","peach") > mode(fruit) [1] "numeric" > length(fruit) [1] 4

  8. 对象属性的转换 • 常用对象转化函数 >as.character() #转换为字符型 >as.numeric() #转换为数值型 >as.logical() #转换为逻辑型 >as.complex() #转化为复数型 >as.factor() #转化为因子型 >methods(as) #methods包中的全部转换函数 >methods(is) #methods包中全部对象类型判别函数

  9. 二、数据的创建

  10. 三、数值向量 1、规则序列 >x<-1:30 >x<-c(1,2,4,7,11) #c() 为应用最广泛的向量构造函数 >y<-c(x,0,x) #把所有对象连接起来形成新的向量 >y<-c(“animal”) >x<-seq(1,5,by=0.5) #序列的起点、终点、步长 x<-seq(length=50,from=-0.2,to=9,by=0.8) >x<-scan() #用键盘输入一些数据 >rep(1,time=30) #创建一个所有元素都相同的向量 >sequence(4:5) #创建一系列联系的整数序列 [1] 1 2 3 4 1 2 3 4 5

  11. >gl(k,n) //k是水平数,n是每个水平重复的次数,有两个选项:length用来指定产生数据的个数,labels用来指定每个水平因子的名字 >gl(3,5) >gl(3,5.4) #???????

  12. 2、随机序列 R可以产生多种不同分布下的随机数序列。 >sample(1:40,5) [1] 25 32 2 35 9 >sample(c("H","T"),10,replace=T) [1] "H" "H" "T" "H" "H" "T" "H" "H" "H" "H“

  13. 3、分布函数 分布函数的形式rfunc(n,p1,p2,...),其中func指概率分布函数,n为生成数据的个数,p1, p2, . . . 是分布的参数数值。 如:rnorm #随机产生正态分布的数据 >rnorm(100,2,5) #mean=1,sd=5 随机分布函数rfunc r:random 密度函数dfunc d:density 累计概率密度函数pfunc p:probability 分位数函数qfunc q:quantile

  14. > plot(rnorm(100,2,5))

  15. > x<-seq(-4,4,0.1) > plot(x,dnorm(x),type="l")

  16. 逻辑向量 • 逻辑向量包含TRUE、FALSE和NA(not availabe) • 逻辑向量可以同数值向量一起运算:TRUE=1,FALSE=0 > SequenceNum<-seq(1.1,4.2) > Logicvector<-SequenceNum>2.5 > logicvector 错误: 找不到对象'logicvector' > Logicvector [1] FALSE FALSE TRUE TRUE

  17. > c(1:3,NA)->NaData • > NaData • [1] 1 2 3 NA • > NaData[8]<-8 #将NaData第8项设置为8 • > NaData • [1] 1 2 3 NA NA NA NA 8 • > is.na(NaData) • [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE

  18. 字符向量 • 可以用两个单引号(‘)或两个双引号(“)去界定字符向量 • “/”用来识别字符串中包含的(‘)或(“) • 例子: > UserInfor<-c("name","user's password","e-mail") > UserInfor [1] "name" "user's password" "e-mail“ > labs<-paste(c("X","Y"),1:10,sep="") > labs [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"

  19. 复数向量 > z <- complex(real = rnorm(10), imaginary = rnorm(10)) > z [1] 1.0464457+1.4921546i -0.0857576+0.6500323i -0.4077956+0.7363948i [4] 1.2992962+1.0821262i -1.3359859-2.2430534i 1.0756972+1.4531898i [7] 1.1084957+0.6604677i 1.2716703+0.4288688i 0.2792403+0.4181003i [10] -1.0414731-2.2300299i > zz <- (rep(1:4,len=9) + 1i*(9:1))/10 [1] 0.1+0.9i 0.2+0.8i 0.3+0.7i 0.4+0.6i 0.1+0.5i 0.2+0.4i 0.3+0.3i 0.4+0.2i [9] 0.1+0.1i > zz.shift <- complex(modulus = Mod(zz), argument= Arg(zz) + pi) > zz.shift [1] -0.1-0.9i -0.2-0.8i -0.3-0.7i -0.4-0.6i -0.1-0.5i -0.2-0.4i -0.3-0.3i [8] -0.4-0.2i -0.1-0.1i

  20. 五、分组因子 • R语言可以将一套数据中相同分类的数据进行分组,所用组别对象称为分组因子 • 可以用factor()函数生成一个分组因子 • 分组因子可以方便地对分组数据做相应计算,如:计算平均值、总和或按某种函数形式 • 例子: • >factor(sample(letters,10,replace=T),levels=letters) [1] g b w j c z o d p q Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

  21. 六、数组及矩阵 • 数组是带多个下标的、类型相同的元素的多维数据集合,类型有数值型、字符型、逻辑型、复数型等 • 矩阵为二维数组 • 一维数组并不等效为向量 • 除了类型和长度等基本属性外,数组还有一个特殊属性叫做维数向量,用dim()定义

  22. 数组定义 >FirstArray<-seq(1:24) > FirstArray [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 > dim(FirstArray)<-c(3,4,2) #通过dim()函数来定义数组的维数空间 >FirstArray #数组的填充规则:越靠前的下标变化越快; #越靠 后的下标变化越慢 , , 1 [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 #等价于 > FirstArray<-array(seq(1:24),dim=c(3,4,2))

  23. Dimnames属性 >Prices<-array(c(20,24,22,56,76,87),dim=c(2,3), #dimnames定义了维名 + dimnames=list(c("vender1","vender2"),c("64M","128M","256M"))) > Prices 64M 128M 256M vender1 20 22 76 vender2 24 56 87 > Prices[,c(“64M”,“256M”)] #通过维名访问数组 64M 256M vender1 20 76 vender2 24 87

  24. 向量,数组的混合运算 规则: • 1、 表达式中各元素匹配时,总是从左到右。 • 2、 在进行计算时比较短的向量会扩展数据以适应最大数量元素的操作数。扩展数据的基本规则为循环地从第一个元素开始填充所需要的数据。 • 3、所有的数组必须具有相同的dim属性,否则返回一个错误。 • 4、任何一个向量的操作结果的长度大于数组间操作结果的长度时,R语言会产生错误。 • 5、 如果数组间操作无误,那么计算结果也是一个相同维度的数组。

  25. 不同dim属性 不同dim属性 相同dim属性 相同dim属性 > a2<-array(1:9,dim=c(4,3)) > a2 [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 1 [3,] 3 7 2 [4,] 4 8 3 > a1+a2 [,1] [,2] [,3] [1,] 2 10 18 [2,] 4 12 11 [3,] 6 14 13 [4,] 8 16 15 > a1*a2 [,1] [,2] [,3] [1,] 1 25 81 [2,] 4 36 10 [3,] 9 49 22 [4,] 16 64 36 > a1<-array(1:12,dim=c(4,3)) > a2<-array(1:9,dim=c(3,3)) > a1 [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > a2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > a1+a2 错误于a1 + a2 : 非整合陈列

  26. 一维数组并不等效为向量 一维数组并不等效为向量 不同长度的向量运算 不同长度的向量运算 > a3<-array(1:3,dim=c(1,3)) > a3 [,1] [,2] [,3] [1,] 1 2 3 > a2+a3 错误于a2 + a3 : 非整合陈列 > a5<-1:7 > a4+a5 [1] 2 4 6 5 7 9 8 警告信息: In a4 + a5 : 长的对象长度不是短的对象长度的整倍数 向量与数组运算 > a6<-1:13 > a6+a2 错误: dims [product 12]与对象长度[13]不匹配 此外: 警告信息: In a6 + a2 : 长的对象长度不是短的对象长度的整倍数 > a7<-1:24 > a7+a2 错误: dims [product 12]与对象长度[24]不匹配 > a4<-1:3 > a4 [1] 1 2 3 > a4+a2 [,1] [,2] [,3] [1,] 2 7 12 [2,] 4 9 2 [3,] 6 8 4 [4,] 5 10 6

  27. 两个数组的外积 > A<-array(1:4,dim=c(2,2)) > B<-array(1:12,dim=c(3,4)) > A [,1] [,2] [1,] 1 3 [2,] 2 4 > B [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > B%o%A , , 1, 1 [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 , , 2, 1 [,1] [,2] [,3] [,4] [1,] 2 8 14 20 [2,] 4 10 16 22 [3,] 6 12 18 24 , , 1, 2 [,1] [,2] [,3] [,4] [1,] 3 12 21 30 [2,] 6 15 24 33 [3,] 9 18 27 36 , , 2, 2 [,1] [,2] [,3] [,4] [1,] 4 16 28 40 [2,] 8 20 32 44 [3,] 12 24 36 48 > AB<-outer(B,A,“*”) #?outer()

  28. 数组转置 • aperm(a,perm,resize=TRUE) #a为数组,perm为转置下标向量 > x<-array(1:6,2:3) > x [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > xt<-aperm(x,c(2,1)) > xt [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6

  29. > xt <- aperm(x, c(2,1,3)) > xt , , 1 [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 , , 2 [,1] [,2] [1,] 7 8 [2,] 9 10 [3,] 11 12 , , 3 [,1] [,2] [1,] 13 14 [2,] 15 16 [3,] 17 18 , , 4 [,1] [,2] [1,] 19 20 [2,] 21 22 [3,] 23 24 > x <- array(1:24, 2:4) > x , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 , , 3 [,1] [,2] [,3] [1,] 13 15 17 [2,] 14 16 18 , , 4 [,1] [,2] [,3] [1,] 19 21 23 [2,] 20 22 24

  30. 七、矩阵 # nrow定义行数,ncol定义列数,dimnames定义行和列的名称,byrow定义矩阵的填充顺序,为T时按行填充,为F时按列填充 >mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE, +dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3"))) > mdat C.1 C.2 C.3 row1 1 2 3 row2 11 12 13 转置 转置 > xt <- aperm(mdat, c(2,1)) > xt row1 row2 C.1 1 11 C.2 2 12 C.3 3 13 > xt <- aperm(mdat, c(2,1)) > xt row1 row2 C.1 1 11 C.2 2 12 C.3 3 13 > t(mdat) row1 row2 C.1 1 11 C.2 2 12 C.3 3 13 > t(mdat) row1 row2 C.1 1 11 C.2 2 12 C.3 3 13

  31. 两个矩阵内积 > a1<-matrix(c(1,2,3,4,5,6),nrow=2,ncol=3) > a2<-matrix(seq(1:9),nrow=3,ncol=3) > a1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > a2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > a1%*%a2 [,1] [,2] [,3] [1,] 22 49 76 [2,] 28 64 100 > a1%*%a1 错误于a1 %*% a1 : 非整合参数 > a2%*%a1 错误于a2 %*% a1 : 非整合参数

  32. 求解线性方程组: 3a-2b=12 2a+b=1 > Coefficient<-array(c(3,2,-2,1),dim=c(2,2)) #系数矩阵 > Result<-c(12,1) #结果矩阵 > solve(Coefficient,Result) #sovle()返回线性方程组行列式结果 [1] 2 -3 #a=2,b=-3 > solve(Coefficient) #求矩阵Coefficient的逆 [,1] [,2] [1,] 0.1428571 0.2857143 [2,] -0.2857143 0.4285714

  33. 矩阵计算函数 练习2.1熟练掌握矩阵计算的有关函数

  34. 数组矩阵合并 • 函数rbind()把参数变量按行拼成一个大矩阵,两个数组(或向量)的列数必须相等 • 函数cbind()把参数变量按列拼成一个大矩阵,两个数组(或向量)的行数必须相等

  35. > a1<-array(1:6,dim=c(2,3)) > a2<-array(1:9,dim=c(3,3)) > a1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > a2 [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 > rbind(a1,a2) [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [3,] 1 4 7 [4,] 2 5 8 [5,] 3 6 9 > cbind(a1,a2) 错误于cbind(a1, a2) : 矩阵的行数必需相符(见arg2) > t(a1) [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 > a1<-t(a1) > cbind(a1,a2) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 1 4 7 [2,] 3 4 2 5 8 [3,] 5 6 3 6 9

  36. 八、数据分割与选取 • 向量索引 X[n] #第n个元素 X[-n] #除了第n个元素外的X X[1:n] #前n个元素 X[-(1:n)] #第n+1至最后的元素 X[c(1,4,2)] #指定下标的元素 X[“name”] #名为name的元素 X[X>3] #所有大于3的元素 X[X>3 & X<=5] #区间(3,5]的元素 X[X %in% c(“a”,”and”,”the”)] #给定组c()中的元素

  37. 例子 > X<-1:9 > names(X)<-letters[1:9] > X a b c d e f g h i 1 2 3 4 5 6 7 8 9 > X[6] f 6 > X[2:6] b c d e f 2 3 4 5 6 > X[-3] a b d e f g h i 1 2 4 5 6 7 8 9 > X[-(5:9)] a b c d 1 2 3 4 > X[-c(5,9,2)] a c d f g h 1 3 4 6 7 8 > X[X>5] f g h i 6 7 8 9 > X[X>5 & X<=8] f g h 6 7 8 > X[c("c","g","d")] c g d 3 7 4 > X[X %in% letters[5:13]] named integer(0) > X[X %in% 5:13] e f g h i 5 6 7 8 9 > sstr <- c("c","ab","B","bba","c","@","bla","a","Ba","%") > sstr[sstr %in% c(letters,LETTERS)] [1] "c" "B" "c" "a"

  38. 矩阵索引 • X[i,j] #下标为(i,j)的元素 • X[i,] #第i列 • X[,j] #第j列 • X[,c(1,3)] #第1,3列 • X[“name”,] #名为”name”的行 数组具有类似的数据分割与选取方法 练习2.2 熟练掌握数据分割与选取的方法

  39. > X<-matrix(1:12,nrow=3,ncol=4,byrow=T,dimnames=list(c(letters[1:3]),LETTERS[4:7])) > X D E F G a 1 2 3 4 b 5 6 7 8 c 9 10 11 12 > X[2,3] [1] 7 > X[2,] D E F G 5 6 7 8 > X[,4] a b c 4 8 12 > X[,c(1,3)] D F a 1 3 b 5 7 c 9 11 > X["a",] D E F G 1 2 3 4

  40. 九、数据列表 • 数据列表(list)是一组数据元素的集合,这些数据元素可以是不同的数据结构(dim属性,数据类型等) • list()函数可以组合任意对象

  41. 例子 > FamilyInfo<-list(hostname="joe",wife="rose",no.children=3,child.ages=c(1,3,7)) > FamilyInfo $hostname [1] "joe" $wife [1] "rose" $no.children [1] 3 $child.ages [1] 1 3 7 > FamilyInfo$wife #通过元素的名字来访问 [1] "rose“ > FamilyInfo[3] #通过元素的编号来访问 $no.children [1] 3 > FamilyInfo[[3]] [1] 3

  42. 数据列表操作 • 长度扩展 > length(FamilyInfo) [1] 4 > FamilyInfo[5]<-list(address=c("10th F, HongXing Building, No.100, JainYe Road")) > FamilyInfo $hostname [1] "joe" $wife [1] "rose" $no.children [1] 3 $child.ages [1] 1 3 7 [[5]] [1] "10th F, HongXing Building, No.100, JainYe Road"

  43. > FamilyBirthday<-list(hostbirthday="1979/10/08",wifebirthday="1973/07/09") > c(FamilyInfo,FamilyBirthday)->Family #函数c()可以连接数据列表 > Family $hostname [1] "joe" $wife [1] "rose" $no.children [1] 3 $child.ages [1] 1 3 7 [[5]] [1] "10th F, HongXing Building, No.100, JainYe Road" $hostbirthday [1] "1979/10/08" $wifebirthday [1] "1973/07/09" 数据列表合并

  44. 十、数据框 数据框(data frame)是一个属于"data.frame" 类的列表。不过,对于可能属于数据框的列表对象有下面一些限制条件, 1、分量必须是向量(数值, 字符, 逻辑),因子,数值矩阵,列表或者其他数据框; 2、矩阵,列表和数据框为新的数据框提供了尽可能多的变量,因为它们各自拥有列,元素或者变量; 3、数值向量,逻辑值,因子保持原有格式,而字符向量会被强制转换成因子并且它的水平就是向量中出现的独立值; 4、在数据框中以变量形式出现的向量结构必须长度一致,矩阵结构必须有一样的行数. 数据框常常会被看作是一个由不同模式和属性的列构成的矩阵。它能以矩阵形式出现,行列可以通过矩阵的索引习惯访问。

  45. 创建数据框 > L3 <- LETTERS[1:3] > d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10, replace=TRUE)) > d x y fac 1 1 1 C 2 1 2 A 3 1 3 B 4 1 4 C 5 1 5 A 6 1 6 B 7 1 7 B 8 1 8 B 9 1 9 B 10 1 10 A > d[1] x 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 > d[[1]] [1] 1 1 1 1 1 1 1 1 1 1 > mode(d[1]) [1] "list" > mode(d[[1]]) [1] "numeric"   > length(d[1]) [1] 1 > length(d[[1]]) [1] 10 > d[[2]][3] [1] 3

  46. >d <- data.frame(cbind(x=1, y=1:10), + fac=sample(L3, 10, replace=TRUE), + HT=sample(c("H","T"),10,replace=T)) > d x y fac HT 1 1 1 B T 2 1 2 A H 3 1 3 B H 4 1 4 A T 5 1 5 C T 6 1 6 A T 7 1 7 B H 8 1 8 C T 9 1 9 C H 10 1 10 A T > rbind(class=sapply(d, class), mode=sapply(d, mode)) x y fac HT class "numeric" "numeric" "factor" "factor" mode "numeric" "numeric" "numeric" "numeric"

  47. 函数I(): Change the class of an object to indicate that it should be treated ‘as is’ > d <- data.frame(cbind(x=1, y=1:10), fac=I(sample(L3, 10, replace=TRUE)),HT=sample(c("H","T"),10,replace=T)) > d x y fac HT 1 1 1 A H 2 1 2 A T 3 1 3 C T 4 1 4 A T 5 1 5 B H 6 1 6 C H 7 1 7 C T 8 1 8 A H 9 1 9 A T 10 1 10 C H > rbind(class=sapply(d, class), mode=sapply(d, mode)) x y fac HT class "numeric" "numeric" "AsIs" "factor" mode "numeric" "numeric" "character" "numeric" > d[[3]] [1] "A" "A" "C" "A" "B" "C" "C" "A" "A" "C" > d[[4]] [1] H T T T H H T H T H Levels: H T

  48. 十一、数据运算

  49. 练习2.3 熟练掌握数据运算的有关符号和函数

More Related