为了账号安全,请及时绑定邮箱和手机立即绑定

R中数据结构与数据的输入

标签:
Java

知乎:parkson

关于对数据的理解,大众理解可能就是一些数字,但是一个人的日常行为包括购物,饮食,足迹,都是数据,总结来说,不管是数字还是行为习惯,凡是能反映信息的都是数据;这是在第一关中对于数据概念的阐述。

R是对数据加工的一个工具,包括分析,可视化等。我们回归到本质,这些数据在R中是如何储存、读取、运用的?这真是我们本关所要讨论学习的。①数据结构②数据的输入

一、数据结构

R拥有的多种用于存储数据的对象类型,包括:标量、向量(concatenate)、矩阵(matrix)、数组(array)、数据框(data. frame)、列表(list),数据以这几种类型被存储起来。

注:R中没有标量,标量只是以单元素向量的形式出现。

① 标量

标量是只含有一个元素的向量,例如

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

`> a<-3

a
[1] 3
b<-"welcome to R"
b
[1] "welcome to R"
c<-TRUE
c
[1] TRUE`

</pre>

② 向量c()

向量是可以存储数值型、字符型或逻辑型数据的一维数组,单个向量中的数据必须拥有相同的数据类型。

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

`> a1<-c(12,23,33,43)

a1
[1] 12 23 33 43
mode(a1)#数值型
[1] "numeric"

a2<-c("hello","hi","a")
a2
[1] "hello" "hi"    "a"
mode(a2)#字符型
[1] "character"

a3<-c(TRUE,FALSE,FALSE)
a3
[1]  TRUE FALSE FALSE
mode(a3)#逻辑型
[1] "logical"`

</pre>

③ 矩阵matrix()

函数matrix(vector,nrow,ncol,byrow,dimnames=list(rnames,cnames))

矩阵是二维的,和向量类似,矩阵中也仅能包含一种数据类型。

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

> matrix(1:20,nrow=4,ncol=5) [,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

</pre>

矩阵中默认按列填充,byrow=FALSE(如上,一般不写),手动设置元素按行填充命令:byrow=TRUE

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

> matrix(1:20,nrow=4,ncol=5,byrow=TRUE) [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 6 7 8 9 10 [3,] 11 12 13 14 15 [4,] 16 17 18 19 20

</pre>

我们对矩阵的行列进行手动命名:

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

`> t<-c(1,3,5,7,8,9,4,6,2,3,9,0,4,3,2,5)

rnames<-c("A1","A2","A3","A4")
cnames<-c("B1","B2","B3","B4")
u<-matrix(t,4,4,byrow=TRUE,dimnames=list(rnames,cnames))
u
B1 B2 B3 B4
A1  1  3  5  7
A2  8  9  4  6
A3  2  3  9  0
A4  4  3  2  5`

</pre>

注:向量是一维的,矩阵是二维的,向量和矩阵中的元素只能是一种数据类型。

④ 数组array()

函数array(vector,dimensions,dimnames),vector是数组中的元素,dimensions是一个数值型向量,规定各个维度下标的最大值。

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

`> q<-array(1:24,c(2,3,4),dimnames=list(c("A1","A2"),c("B1","B2","B3"),c("C1","C2","C3","C4")))#c(2,3,4),在空间坐标系里面可以理解为:x坐标是2,y坐标是3,z坐标是4

q
, , C1

B1 B2 B3
A1  1  3  5
A2  2  4  6

, , C2

B1 B2 B3
A1  7  9 11
A2  8 10 12

, , C3

B1 B2 B3
A1 13 15 17
A2 14 16 18

, , C4

B1 B2 B3
A1 19 21 23
A2 20 22 2`

</pre>

对于向量、矩阵、数组的理解为笛卡尔坐标系,一维,二维,三维。且和向量矩阵一样,数组中的数据也只能拥有一种数据类型。

⑤ 数据框data.frame()

数据框中不同的列可以包含不同类型的数据(数值型,字符型,逻辑型等),同一列要求具有相同类型的数据。

直接调用了R安装自带的一个数据文件,数据框的格式如下。

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

> mtcars mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

</pre>

⑥ 列表list()

列表是一些对象的有序集合,允许你整合若干对象到单个对象名下。一个列表中可能是若干向量、举证、数据框的组合。

函数list()创建列表。

二、数据的输入

第一部分我们了解数据在R中存储的结构,第二部分我们学习如何将数据导入R中,包括:

①:键盘输入数据

R中的函数edit()会自动调用一个允许手动输入数据的文本编辑器。

例如:

<pre style="margin-top: 0px !important; margin-right: 0px; margin-bottom: 0px !important; margin-left: 0px; padding: 6px 10px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; line-height: 19px; overflow: auto; border-radius: 3px; background-color: rgb(248, 248, 248);">

`> firstdata<-data.frame(dose=numeric(0),drugA=numeric(0),drugB=numeric(0))

mydata<-edit(firstdata)`

</pre>



<figure style="margin: 1em 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word !important; font-size: 17px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 0.544000029563904px; line-height: 27.2000007629395px; orphans: auto; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(26, 26, 26); font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Microsoft YaHei', 'Source Han Sans SC', 'Noto Sans CJK SC', 'WenQuanYi Micro Hei', sans-serif; background-color: rgb(255, 255, 255);">

webp



作者:天善智能
链接:https://www.jianshu.com/p/dc84d4d5a81c


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消