R语言列表和数据框的具体使用

来自:网络
时间:2023-05-18
阅读:
目录

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"  
返回顶部
顶部