一.數(shù)據(jù)集的概念
數(shù)據(jù)集通常由數(shù)據(jù)構(gòu)成的一個(gè)矩形數(shù)組皿伺,行表觀測雁芙,列表變量。當(dāng)然不同行業(yè)對(duì)數(shù)據(jù)集的行列叫法不同。
①統(tǒng)計(jì)學(xué)家稱它們?yōu)橛^測和變量
②數(shù)據(jù)分析師稱記錄和字段
③機(jī)器學(xué)習(xí)算法工程師稱其為實(shí)例和屬性
2.2數(shù)據(jù)結(jié)構(gòu)
R用于存儲(chǔ)數(shù)據(jù)的對(duì)象類型嚷闭,包括標(biāo)量、向量赖临、矩陣胞锰、數(shù)組、數(shù)據(jù)框和列表:
2.2.1 向量
向量是用于儲(chǔ)存數(shù)值型兢榨,字符型嗅榕,邏輯型數(shù)據(jù)的一維數(shù)組。函數(shù)c()創(chuàng)建向量吵聪。
a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("one", "two", "three")
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)
這里凌那,a是數(shù)值型向量,b是字符型向量吟逝,而c是邏輯型向量帽蝶。注意,單個(gè)向量中的數(shù)據(jù)必須擁有相同的類型或模式(數(shù)值型块攒、字符型或邏輯型)励稳。同一向量中無法混雜不同模式的數(shù)據(jù)。
通過在方括號(hào)中給定元素所處位置的數(shù)值局蚀,我們可以訪問向量中的元素。
注意:標(biāo)量是只含有一個(gè)元素的向量f<-7
a <- c("k", "j", "h", "a", "c", "m")
a[3]
"h"
a[c(1, 3, 5)]
"k" "h" "c"
a[2:6]
"j" "h" "a" "c" "m"
2.2.2 矩陣(二維數(shù)組)
矩陣是一個(gè)二維數(shù)組恕稠,只是每個(gè)元素都擁有相同的模式(數(shù)值型琅绅、字符型或邏輯型)《煳。可通過函數(shù)matrix( )
創(chuàng)建矩陣千扶。使用格式:
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns, byrow=logical_value, dimnames=list( char_vector_rownames, char_vector_colnames))
其中vector
包含了矩陣的元素,nrow
和ncol
用以指定行和列的維數(shù)骆捧,dimnames
包含了可選的澎羞、以字符型向量表示的行名和列名。選項(xiàng)byrow
則表明矩陣應(yīng)當(dāng)按行填充(byrow=TRUE
)還是按列填充(byrow=FALSE
)敛苇,默認(rèn)情況下按列填充妆绞。
代碼清單2-1:創(chuàng)建矩陣
y <- matrix(1:20, nrow=5, ncol=4) #創(chuàng)建一個(gè)5*4矩陣
y
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
cells <- c(1,26,24,68)
rnames <- c("R1", "R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames)) #按行填充的2*2矩陣
mymatrix
C1 C2
R1 1 26
R2 24 68
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE, dimnames=list(rnames, cnames)) #按列填充的2*2矩陣
mymatrix
C1 | C2 | |
---|---|---|
R1 | 1 | 24 |
R2 | 26 | 68 |
代碼清單2-2: 矩陣下標(biāo)的使用
我們可以使用下標(biāo)和方括號(hào)來選擇矩陣中的行、列或元素枫攀。X[i,]
指矩陣 X 中的第i行括饶,X[,j]
指第 j 列,X[i,j]
指第 i 行第 j 個(gè)元素来涨。選擇多行或多列時(shí)图焰,下標(biāo) i 和 j 可為數(shù)值型向量
In [8]:
x <- matrix(1:10, nrow=2)
x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
x[2,]
[1] 2 4 6 8 10
x[,2]
[1] 3 4
x[1,4]
[1] 7
x[1, c(4,5)]
[1] 7 9
數(shù)組
數(shù)組(array)與矩陣類似,但是維度可以大于2蹦掐。數(shù)組可通過array
函數(shù)創(chuàng)建, 形式如下: myarray <- array(vector, dimensions, dimnames)
其中vector
包含了數(shù)組中的數(shù)據(jù)技羔,dimensions
是一個(gè)數(shù)值型向量僵闯,給出了各個(gè)維度下標(biāo)的最大值,而dimnames
是可選的藤滥、各維度名稱標(biāo)簽的列表鳖粟。
代碼清單2-3: 創(chuàng)建一個(gè)數(shù)組
dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3")
dim3 <- c("C1", "C2", "C3", "C4")
z <- array(1:24, c(2,3,4), dimnames=list(dim1, dim2, dim3))
print(z)
, , 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 24
數(shù)據(jù)框
代碼清單2-4: 創(chuàng)建一個(gè)數(shù)據(jù)框
數(shù)據(jù)框可通過函數(shù)data.frame()
創(chuàng)建: mydata <- data.frame(col1, col2, col3,...)
其中的列向量 col1
、col2
超陆、col3
等可為任何類型(如字符型牺弹、數(shù)值型或邏輯型)。每一列的名稱可由函數(shù)names
指定时呀。
In [15]:
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
patientdata
patientID | age | diabetes | status |
---|---|---|---|
1 | 25 | Type1 | Poor |
2 | 34 | Type2 | Improved |
3 | 28 | Type1 | Excellent |
4 | 52 | Type1 | Poor |
代碼清單2-5: 選取數(shù)據(jù)框中的元素
可以使用前述(如矩陣中的)下標(biāo)記號(hào)张漂,亦可直接指定列名
patientdata[1:2]
patientID | age |
---|---|
1 | 25 |
2 | 34 |
3 | 28 |
4 | 52 |
patientdata[c("diabetes","status")]
diabetes | status |
---|---|
Type1 | Poor |
Type2 | Improved |
Type1 | Excellent |
Type1 | Poor |
patientdata$age
25 34 28 52
table(patientdata$diabetes, patientdata$status)#生成diabetes和status的列聯(lián)表
Excellent Improved Poor
Type1 1 0 2
Type2 0 1 0
因子
代碼清單2-6 因子的使用
變量:名義型、有序型或連續(xù)型變量 谨娜。
類別(名義型)變量和有序類別(有序型)變量在R中稱為因子(factor) 航攒。函數(shù)factor( )以一個(gè)整數(shù)向量的形式存儲(chǔ)類別值,整數(shù)的取值范圍是[1... k ](其中k 是名義型變量中唯一值的個(gè)數(shù))趴梢,同時(shí)一個(gè)由字符串(原始值)組成的內(nèi)部向量將映射到這些整數(shù)上漠畜。
①名義型:diabetes<-factor(diabetes)
②有序型:status<-factor(status,order=T)#表示有序型變量,需要為函數(shù)factor()指定參數(shù)order=TRUE 坞靶。
代碼清單2-6演示了普通因子和有序因子的不同是如何影響數(shù)據(jù)分析的憔狞。
# 以向量形式輸入數(shù)據(jù)
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
diabetes <- factor(diabetes) #普通因子
status <- factor(status, order=TRUE) #有序型因子
patientdata <- data.frame(patientID, age, diabetes, status)
#顯示對(duì)象的結(jié)構(gòu)
str(patientdata)
'data.frame': 4 obs. of 4 variables:
$ patientID: num 1 2 3 4
$ age : num 25 34 28 52
$ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1
$ status : Ord.factor w/ 3 levels "Excellent"<"Improved"<..: 3 2 1 3
# 顯示對(duì)象的統(tǒng)計(jì)概要
summary(patientdata)
patientID age diabetes status
Min. :1.00 Min. :25.00 Type1:3 Excellent:1
1st Qu.:1.75 1st Qu.:27.25 Type2:1 Improved :1
Median :2.50 Median :31.00 Poor :2
Mean :2.50 Mean :34.75
3rd Qu.:3.25 3rd Qu.:38.50
Max. :4.00 Max. :52.00
列表
列表(list)是R的數(shù)據(jù)類型中最為復(fù)雜的一種。一般來說彰阴,列表就是一些對(duì)象(或成分瘾敢,component)的有序集合。列表允許你整合若干(可能無關(guān)的)對(duì)象到單個(gè)對(duì)象名下尿这。
可以使用函數(shù)list()
創(chuàng)建列表:mylist <- list(object1, object2, ...)
其中的對(duì)象可以是目前為止講到的任何結(jié)構(gòu)簇抵。
你還可以為列表中的對(duì)象命名:mylist <- list(name1=object1, name2=object2, ...)
代碼清單2-7: 創(chuàng)建一個(gè)列表
g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow=5)
k <- c("one", "two", "three")
mylist <- list(title=g, ages=h, j, k)
# 輸出整個(gè)列表
print(mylist)
$title
[1] "My First List"
$ages
[1] 25 26 18 39
[[3]]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
[[4]]
[1] "one" "two" "three"