1. 程式人生 > >R中資料結構與資料的輸入

R中資料結構與資料的輸入

作者:徐濤,19年應屆畢業生,專注於珊瑚礁研究,喜歡用R各種清洗資料。

知乎: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);"> 3901436-5e88d9ef1d34adac image

</figure>

注:(1)建立一個空資料框(或矩陣),其中變數名和變數的模式需與理想中的最終資料一致。

(2)呼叫文字編輯器,輸入資料。

②:匯入txt檔案

我一般喜歡將需要匯入的檔案直接放在工作目錄裡面(工作目錄不要出現漢字)

<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);">

`> #查詢當前工作目錄

getwd()
[1] "C:/Users/徐濤/Documents"

改變工作目錄

setwd("E:/Data For R/RData")

查詢當前工作目錄

getwd()
[1] "E:/Data For R/RData"
a<-read.table("income.txt")
a
V1 V2 V3
1 t x y
2 1990 2822 2937
3 1991 2990 3149
4 1992 3297 3483
5 1993 4225 4349
6 1994 5127 5218
7 1995 6038 6242
8 1996 6910 7408
9 1997 8234 8651
10 1998 9363 9876
11 1999 10683 11444
12 2000 12582 13395
13 2001 15301 16386`

</pre>

③:匯入excel資料

<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);">

`> install.packages("xlsx")
--- 在此連線階段時請選用CRAN的鏡子 ---
還安裝相依關係‘rJava’, ‘xlsxjars’

程式包‘rJava’開啟成功,MD5和檢查也通過
程式包‘xlsxjars’開啟成功,MD5和檢查也通過
程式包‘xlsx’開啟成功,MD5和檢查也通過

下載的二進位制程式包在
C:\Windows\Temp\Rtmp8G83Je\downloaded_packages裡

library(xlsx)
y<-read.xlsx(income,1)`

</pre>

一句話送給大家:很多時候方向比努力更重要,一個正確的學習方法+一個明確的方向會讓事情或者學習事半功倍。