r语言列表和数据框的具体使用-kb88凯时官网登录

来自:网络
时间:2023-05-18
阅读:
免费资源网 - https://freexyz.cn/
目录

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"  
免费资源网 - https://freexyz.cn/
返回顶部
顶部
网站地图