R語言列表和數據框的具體使用
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語言列表和數據框 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- 前端elementUI select選擇器實現遠程搜索
- Pandas 模糊查詢與替換的操作
- R語言數據框合並(merge)的幾種方式小結
- Java圖形化界面設計之佈局管理器之BorderLayout案例詳解
- 深入理解最新Python中的Match Case