本篇内容主要讲解“R语言列表和数据框怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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 下标访问
> 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
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
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.area1 Alabama AL 516092 Alaska AK 5897573 Arizona AZ 1139094 Arkansas AR 531045 California CA 1586936 Colorado CO 1042477 Connecticut CT 50098 Delaware DE 20579 Florida FL 5856010 Georgia GA 5887611 Hawaii HI 645012 Idaho ID 8355713 Illinois IL 5640014 Indiana IN 3629115 Iowa IA 5629016 Kansas KS 8226417 Kentucky KY 4039518 Louisiana LA 4852319 Maine ME 3321520 Maryland MD 1057721 Massachusetts MA 825722 Michigan MI 5821623 Minnesota MN 8406824 Mississippi MS 4771625 Missouri MO 6968626 Montana MT 14713827 Nebraska NE 7722728 Nevada NV 11054029 New Hampshire NH 930430 New Jersey NJ 783631 New Mexico NM 12166632 New York NY 4957633 North Carolina NC 5258634 North Dakota ND 7066535 Ohio OH 4122236 Oklahoma OK 6991937 Oregon OR 9698138 Pennsylvania PA 4533339 Rhode Island RI 121440 South Carolina SC 3105541 South Dakota SD 7704742 Tennessee TN 4224443 Texas TX 26733944 Utah UT 8491645 Vermont VT 960946 Virginia VA 4081547 Washington WA 6819248 West Virginia WV 2418149 Wisconsin WI 5615450 Wyoming WY 97914>
2.2 访问
2.1 下标访问
> state[1] state.name1 Alabama2 Alaska3 Arizona4 Arkansas5 California6 Colorado7 Connecticut8 Delaware9 Florida10 Georgia11 Hawaii12 Idaho13 Illinois14 Indiana15 Iowa16 Kansas17 Kentucky18 Louisiana19 Maine20 Maryland21 Massachusetts22 Michigan23 Minnesota24 Mississippi25 Missouri26 Montana27 Nebraska28 Nevada29 New Hampshire30 New Jersey31 New Mexico32 New York33 North Carolina34 North Dakota35 Ohio36 Oklahoma37 Oregon38 Pennsylvania39 Rhode Island40 South Carolina41 South Dakota42 Tennessee43 Texas44 Utah45 Vermont46 Virginia47 Washington48 West Virginia49 Wisconsin50 Wyoming
2.2 名称访问
> state["state.name"] state.name1 Alabama2 Alaska3 Arizona4 Arkansas5 California6 Colorado7 Connecticut8 Delaware9 Florida10 Georgia11 Hawaii12 Idaho13 Illinois14 Indiana15 Iowa16 Kansas17 Kentucky18 Louisiana19 Maine20 Maryland21 Massachusetts22 Michigan23 Minnesota24 Mississippi25 Missouri26 Montana27 Nebraska28 Nevada29 New Hampshire30 New Jersey31 New Mexico32 New York33 North Carolina34 North Dakota35 Ohio36 Oklahoma37 Oregon38 Pennsylvania39 Rhode Island40 South Carolina41 South Dakota42 Tennessee43 Texas44 Utah45 Vermont46 Virginia47 Washington48 West Virginia49 Wisconsin50 Wyoming
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.4 函数访问
> attach(state)The following objects are masked from package:datasets:
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语言列表和数据框怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!