R语言列表和数据框的具体使用
程序员文章站
2022-06-14 23:20:38
目录1.列表1.1创建1.2 访问1.3 注意2.数据框2.1 创建2.2 访问1.列表列表“list”是一种比较的特别的对象集合,不同的序号对于不同的元素,当然元素的也可以是...
1.列表
列表“list”是一种比较的特别的对象集合,不同的序号对于不同的元素,当然元素的也可以是不同类型的,那么我们用r语言先简单来构造一个列表。
1.1创建
> a<-c(1:20) > b<-matrix(1:20,4,5) > mlist<-list(a,b) > mlist [[1]] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [15] 15 16 17 18 19 20 [[2]] [,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
1.2 访问
1.2.1 下标访问
> mlist[1] [[1]] [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [15] 15 16 17 18 19 20 > mlist[2] [[1]] [,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
1.2.2 名称访问
> state.center["x"] $x [1] -86.7509 -127.2500 -111.6250 -92.2992 [5] -119.7730 -105.5130 -72.3573 -74.9841 [9] -81.6850 -83.3736 -126.2500 -113.9300 [13] -89.3776 -86.0808 -93.3714 -98.1156 [17] -84.7674 -92.2724 -68.9801 -76.6459 [21] -71.5800 -84.6870 -94.6043 -89.8065 [25] -92.5137 -109.3200 -99.5898 -116.8510 [29] -71.3924 -74.2336 -105.9420 -75.1449 [33] -78.4686 -100.0990 -82.5963 -97.1239 [37] -120.0680 -77.4500 -71.1244 -80.5056 [41] -99.7238 -86.4560 -98.7857 -111.3300 [45] -72.5450 -78.2005 -119.7460 -80.6665 [49] -89.9941 -107.2560
1.2.3 符号访问
> state.center$x [1] -86.7509 -127.2500 -111.6250 -92.2992 [5] -119.7730 -105.5130 -72.3573 -74.9841 [9] -81.6850 -83.3736 -126.2500 -113.9300 [13] -89.3776 -86.0808 -93.3714 -98.1156 [17] -84.7674 -92.2724 -68.9801 -76.6459 [21] -71.5800 -84.6870 -94.6043 -89.8065 [25] -92.5137 -109.3200 -99.5898 -116.8510 [29] -71.3924 -74.2336 -105.9420 -75.1449 [33] -78.4686 -100.0990 -82.5963 -97.1239 [37] -120.0680 -77.4500 -71.1244 -80.5056 [41] -99.7238 -86.4560 -98.7857 -111.3300 [45] -72.5450 -78.2005 -119.7460 -80.6665 [49] -89.9941 -107.2560
1.3 注意
一个中括号和两个中括号的区别
一个中括号输出的是列表的一个子列表,两个中括号输出的是列表的元素
> class(mlist[1]) [1] "list" > class(mlist[[1]]) [1] "integer"
我们添加元素时要注意用两个中括号
2.数据框
数据框是r种的一个数据结构,他通常是矩阵形式的数据,但矩阵各列可以是不同类型的,数据框每列是一个变量,没行是一个观测值。
但是,数据框又是一种特殊的列表对象,其class属性为“data.frame”,各列表成员必须是向量(数值型、字符型、逻辑型)、因子、数值型矩阵、列表或者其它数据框。向量、因子成员为数据框提供一个变量,如果向量非数值型会被强型转换为因子。而矩阵、列表、数据框等必须和数据框具有相同的行数。
2.1 创建
> state<-data.frame(state.name,state.abb,state.area) > state state.name state.abb state.area 1 alabama al 51609 2 alaska ak 589757 3 arizona az 113909 4 arkansas ar 53104 5 california ca 158693 6 colorado co 104247 7 connecticut ct 5009 8 delaware de 2057 9 florida fl 58560 10 georgia ga 58876 11 hawaii hi 6450 12 idaho id 83557 13 illinois il 56400 14 indiana in 36291 15 iowa ia 56290 16 kansas ks 82264 17 kentucky ky 40395 18 louisiana la 48523 19 maine me 33215 20 maryland md 10577 21 massachusetts ma 8257 22 michigan mi 58216 23 minnesota mn 84068 24 mississippi ms 47716 25 missouri mo 69686 26 montana mt 147138 27 nebraska ne 77227 28 nevada nv 110540 29 new hampshire nh 9304 30 new jersey nj 7836 31 new mexico nm 121666 32 new york ny 49576 33 north carolina nc 52586 34 north dakota nd 70665 35 ohio oh 41222 36 oklahoma ok 69919 37 oregon or 96981 38 pennsylvania pa 45333 39 rhode island ri 1214 40 south carolina sc 31055 41 south dakota sd 77047 42 tennessee tn 42244 43 texas tx 267339 44 utah ut 84916 45 vermont vt 9609 46 virginia va 40815 47 washington wa 68192 48 west virginia wv 24181 49 wisconsin wi 56154 50 wyoming wy 97914 >
2.2 访问
2.2.1 下标访问
> state[1] state.name 1 alabama 2 alaska 3 arizona 4 arkansas 5 california 6 colorado 7 connecticut 8 delaware 9 florida 10 georgia 11 hawaii 12 idaho 13 illinois 14 indiana 15 iowa 16 kansas 17 kentucky 18 louisiana 19 maine 20 maryland 21 massachusetts 22 michigan 23 minnesota 24 mississippi 25 missouri 26 montana 27 nebraska 28 nevada 29 new hampshire 30 new jersey 31 new mexico 32 new york 33 north carolina 34 north dakota 35 ohio 36 oklahoma 37 oregon 38 pennsylvania 39 rhode island 40 south carolina 41 south dakota 42 tennessee 43 texas 44 utah 45 vermont 46 virginia 47 washington 48 west virginia 49 wisconsin 50 wyoming
2.2.2 名称访问
> state["state.name"] state.name 1 alabama 2 alaska 3 arizona 4 arkansas 5 california 6 colorado 7 connecticut 8 delaware 9 florida 10 georgia 11 hawaii 12 idaho 13 illinois 14 indiana 15 iowa 16 kansas 17 kentucky 18 louisiana 19 maine 20 maryland 21 massachusetts 22 michigan 23 minnesota 24 mississippi 25 missouri 26 montana 27 nebraska 28 nevada 29 new hampshire 30 new jersey 31 new mexico 32 new york 33 north carolina 34 north dakota 35 ohio 36 oklahoma 37 oregon 38 pennsylvania 39 rhode island 40 south carolina 41 south dakota 42 tennessee 43 texas 44 utah 45 vermont 46 virginia 47 washington 48 west virginia 49 wisconsin 50 wyoming
2.2.3 符号访问
> state$state.name [1] "alabama" "alaska" [3] "arizona" "arkansas" [5] "california" "colorado" [7] "connecticut" "delaware" [9] "florida" "georgia" [11] "hawaii" "idaho" [13] "illinois" "indiana" [15] "iowa" "kansas" [17] "kentucky" "louisiana" [19] "maine" "maryland" [21] "massachusetts" "michigan" [23] "minnesota" "mississippi" [25] "missouri" "montana" [27] "nebraska" "nevada" [29] "new hampshire" "new jersey" [31] "new mexico" "new york" [33] "north carolina" "north dakota" [35] "ohio" "oklahoma" [37] "oregon" "pennsylvania" [39] "rhode island" "south carolina" [41] "south dakota" "tennessee" [43] "texas" "utah" [45] "vermont" "virginia" [47] "washington" "west virginia" [49] "wisconsin" "wyoming"
2.2.4 函数访问
> attach(state) the following objects are masked from package:datasets:
2.2.4 函数访问
> attach(state) the following objects are masked from package:datasets: state.abb, state.area, state.name > state.name [1] "alabama" "alaska" [3] "arizona" "arkansas" [5] "california" "colorado" [7] "connecticut" "delaware" [9] "florida" "georgia" [11] "hawaii" "idaho" [13] "illinois" "indiana" [15] "iowa" "kansas" [17] "kentucky" "louisiana" [19] "maine" "maryland" [21] "massachusetts" "michigan" [23] "minnesota" "mississippi" [25] "missouri" "montana" [27] "nebraska" "nevada" [29] "new hampshire" "new jersey" [31] "new mexico" "new york" [33] "north carolina" "north dakota" [35] "ohio" "oklahoma" [37] "oregon" "pennsylvania" [39] "rhode island" "south carolina" [41] "south dakota" "tennessee" [43] "texas" "utah" [45] "vermont" "virginia" [47] "washington" "west virginia" [49] "wisconsin" "wyoming"
到此这篇关于r语言列表和数据框的具体使用的文章就介绍到这了,更多相关r语言列表和数据框 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 手机注册推特账号(手把手教你注册流程)
下一篇: python的图形用户界面介绍
推荐阅读
-
使用vue根据状态添加列表数据和删除列表数据的实例
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
R语言中的多维数组、矩阵、列表、数据框
-
R语言 实现将数据框中的字符类型数字转换为数值
-
SQL语句的数据操作语言 (DML) 和 数据定义语言 (DDL)使用介绍
-
R语言求一行(列表、list)数据的平均数操作
-
使用Ajax和Jquery配合数据库实现下拉框的二级联动的示例
-
ASP.NET MVC下使用AngularJs语言(六):获取下拉列表的value和Text
-
R语言删除/添加数据框中的某一行/列
-
R语言histogram(直方图)的具体使用