2021-03-04
3-4-2 數(shù)據(jù)框
打開02-data-structure.R
options(stringsAsFactors = F)
意思是在生成字符串向量的時(shí)候,不要轉(zhuǎn)換為因子型。
options表示設(shè)置,以前默認(rèn)是T,在R4.0以后版本無需這個(gè)操作了
1.數(shù)據(jù)框來源
(1)在R中用代碼新建數(shù)據(jù)框
(2)由已有數(shù)據(jù)轉(zhuǎn)換或處理得到
(3)從文件中獲取
默認(rèn)表格文件(比較方方正正)讀取到表格中為數(shù)據(jù)框,不方正可能會(huì)報(bào)錯(cuò)
(4)內(nèi)置數(shù)據(jù)集
不需要賦值也可以找到對(duì)象 如:iris為內(nèi)置數(shù)據(jù)
2.新建數(shù)據(jù)框-- 本質(zhì)上是向量案列組合
df <- data.frame(gene = c("gene1","gene2","gene3"),
sam = c("sample1","sample2","sample3"),
exp = c(32,34,45))
data.frame函數(shù)的轄區(qū)()中左側(cè)為列名,右側(cè)為列的內(nèi)容
幾列取決于有幾個(gè)等號(hào),有幾個(gè)列名;
幾行取決于向量的元素個(gè)數(shù);
列與列之間用“,”分隔;
“=”并不是賦值(等號(hào)可以替代賦值符號(hào),而賦值符號(hào)不能替代等號(hào))
也可以寫為:
df <- data.frame(gene = paste0("gene",1:3),
sam = paste0("sample",1:3),
exp = c(32,34,45))
讀取數(shù)據(jù)框
df2 <- read.csv("gene.csv")
由外向內(nèi)寫,注意不要少括號(hào)
如果報(bào)錯(cuò)可能:打開方式錯(cuò)誤,不在正確的工作目錄下;
RProject-File-Open File-找到工作目錄。
> df2 <- read.csv("gene.csv")
> df2
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
3.數(shù)據(jù)框性質(zhì)描述
1.維度(有幾行幾列)
> dim(df)
[1] 3 3 #3行3列
> nrow(df)行
[1] 3 #3行
> ncol(df)列
[1] 3#3列
2. 行名/列名
rownames(df)/colnames(df)
> rownames(df)#行名
[1] "1" "2" "3"
> colnames(df)#列名
[1] "gene" "sam" "exp"
4.數(shù)據(jù)框取子集(一個(gè)、一行、一列、多行多列)
4.1
向量-位置
x[4] 向量中第4個(gè)元素
x{c(1,5)}向量中第1個(gè)和第5個(gè)元素
向量只有一個(gè)維度,因此中括號(hào)中只能是數(shù)值或向量
數(shù)據(jù)框
--坐標(biāo)
df[2,2]
數(shù)據(jù)框不僅有坐標(biāo)即某一個(gè)位置,還可能有某一行或者某一列
--某一行
df[2,] 表示第2行
--某一列
df[ ,2] 表示第2列
中括號(hào)中:逗號(hào)左邊為行,右邊為列,哪邊空著就是哪邊全選
---行列雙選
df[ c(1,3),1:2] 表示第1行和第3行,第1-2列
中括號(hào)里的逗號(hào),表示維度的分隔
4.2根據(jù)行名或列名
當(dāng)行數(shù)和列數(shù)很多時(shí)較為便捷,其實(shí)向量也可以根據(jù)”名字“提取子集
> df <- read.csv("gene.csv")
> df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
> df[,"gene"]
[1] "gene1" "gene2" "gene3"
> df[,c('gene','exp')]
gene exp
1 gene1 32
2 gene2 34
3 gene3 45
>nn=c(c('gene','exp'))
> df[,nn]
gene exp
1 gene1 32
2 gene2 34
3 gene3 45
進(jìn)階
> df[,ncol(df)]# 取最后一列
[1] 32 34 45
> df[,-ncol(df)]#去掉最后一列
gene sam
1 gene1 sample1
2 gene2 sample2
3 gene3 sample3
4.3 提取列的常用操作
數(shù)據(jù)框常用操作:df 前面是數(shù)據(jù)框的名字,$后面是列的名字 ,一次只能取一列)

4.5數(shù)據(jù)框修改
改一個(gè)格
df[3,3]<- 5
改一整列
df$exp<-c(12,23,50)
> df$abc <-c(23,15,37) 新增一列名為ABC,并賦值
> df
gene sam exp abc
1 gene1 sample1 12 23
2 gene2 sample2 23 15
3 gene3 sample3 50 37
誤操作怎么辦?
對(duì)變量進(jìn)行多次賦值,結(jié)果以最后一次為準(zhǔn);
運(yùn)行錯(cuò)誤的代碼,不能撤銷,但可以覆蓋。
改行名和列名
> rownames(df) <- c("r1","r2","r3");df
gene sam exp abc
r1 gene1 sample1 12 23
r2 gene2 sample2 23 15
r3 gene3 sample3 50 37
只修改某一行/列的名:給行名這個(gè)向量的某一個(gè)元素賦值
> rownames(df)[2]="x";df
gene sam exp abc
r1 gene1 sample1 12 23
x gene2 sample2 23 15
r3 gene3 sample3 50 37
練習(xí)3-1
> # 練習(xí)3-1
> # 1.讀取excise.csv這個(gè)文件,賦值給test。
> test<-read.csv("excise.csv")
> # 2.描述test的屬性(行名列名,行數(shù)列數(shù))。
> dim(test)
[1] 15 3
> nrow(test)
[1] 15
> ncol(test)
[1] 3
> # 3.求第一列數(shù)值的中位數(shù)
> median(test[,1])
[1] 4.6
> # 4.修改test前兩列的列名為L(zhǎng)ength和Width
> colnames(test[,1:2]) <- c("length","width")
***5.提取test中,最后一列值為versicolor或setosa的行,組成一個(gè)新的數(shù)據(jù)框,賦值給test2。
3-4-2 數(shù)據(jù)框
6.數(shù)據(jù)框進(jìn)階
(1)行數(shù)較多的數(shù)據(jù)框可截取前/后幾行查看
> iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
11 5.4 3.7 1.5 0.2 setosa
12 4.8 3.4 1.6 0.2 setosa
13 4.8 3.0 1.4 0.1 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
17 5.4 3.9 1.3 0.4 setosa
18 5.1 3.5 1.4 0.3 setosa
19 5.7 3.8 1.7 0.3 setosa
20 5.1 3.8 1.5 0.3 setosa
...
> head(iris)#不指定默認(rèn)看前6行
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> head(iris,3)#指定看前3行
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
(2)行列數(shù)都多的數(shù)據(jù)框可取前幾行前幾列查看
> iris[1:3,1:3]
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
> iris[1:3,1:2]
Sepal.Length Sepal.Width
1 5.1 3.5
2 4.9 3.0
3 4.7 3.2
> class(iris[,1:2])
[1] "data.frame"
> class(iris[,1])# 數(shù)據(jù)框的第一列為向量
[1] "numeric"
> class(iris[1])#若想讓數(shù)據(jù)框一列不變成向量還是保持?jǐn)?shù)據(jù)框結(jié)構(gòu),去掉逗號(hào),即列數(shù)為1的表格
#數(shù)據(jù)框/矩陣都是按列組織的,中括號(hào)中沒有逗號(hào)時(shí),優(yōu)先取列
[1] "data.frame"
(3) 查看每一列的數(shù)據(jù)類型和具體內(nèi)容
> str(df)
'data.frame': 3 obs. of 4 variables:
#3obvious 4variables代表 3行4列
$ gene: chr "gene1" "gene2" "gene3"#chr charactor
$ sam : chr "sample1" "sample2" "sample3"
$ exp : num 12 23 50
$ abc : num 23 15 37
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...#因子
(4)去除含有缺失值的行
> #生成一個(gè)有NA的數(shù)據(jù)框
> df<-data.frame(X1 = LETTERS[1:5],X2 = 1:5)
> df[2,2] <- NA
> df[4,1] <- NA
> df
X1 X2
1 A 1
2 B NA
3 C 3
4 <NA> 4
5 E 5
> na.omit(df)
X1 X2
1 A 1
3 C 3
5 E 5
(5)兩個(gè)表格的鏈接
按列連接:cbind 兩個(gè)表格行數(shù)相同;
按行連接:rbind 兩個(gè)表格列數(shù)相同。
> test1 <- data.frame(name = c('jimmy','nicker','doodle'),
+ blood_type = c("A","B","O"))
> test1
name blood_type
1 jimmy A
2 nicker B
3 doodle O
> test2 <- data.frame(name = c('doodle','jimmy','nicker','Tony'),
+ group = c("group1","group1","group2","group2"),
+ vision = c(4.2,4.3,4.9,4.5))
> test2
name group vision
1 doodle group1 4.2
2 jimmy group1 4.3
3 nicker group2 4.9
4 tony group2 4.5
> test3 <- data.frame(NAME = c('doodle','jimmy','lucy','nicker'),
+ weight = c(140,145,110,138))
merge 相對(duì)更智能,無論列名是否相同,都可以按照具有較差或重疊內(nèi)容那一列對(duì)兩個(gè)表格進(jìn)行整合
> tmp =merge(test1,test2,by="name")#by="name"代表列名相同,列的內(nèi)容有交叉
> merge(x=test1,y=test3,by.x = "name",by.y = "NAME")#列名不同列內(nèi)容有交叉
name blood_type weight
1 doodle O 140
2 jimmy A 145
3 nicker B 138


(6)如果列名順序錯(cuò)亂,如何按照指定順序重排?
https://mp.weixin.qq.com/s/rA92iZS8HUiuwlyrPirHdA
(7)刪除變量
刪除一個(gè)變量
rm(l)
刪除多個(gè)變量
rm(df,m)
刪除全部變量
rm(list = ls())
清除控制臺(tái)ctrl+l等于environment里面的小掃把
矩陣新建和取子集
> m <- matrix(1:9, nrow = 3)
> colnames(m) <- c("a","b","c") #列名
> m
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> #整行
> m[2,]
a b c
2 5 8
> #整列**矩陣不支持用$取子集,只支持[ , ] 的左右邊
> m[,1]
[1] 1 2 3
> #單個(gè)格
> m[2,3]
c
8
> #多個(gè)格
> m[2:3,1:2]
a b
[1,] 2 5
[2,] 3 6
矩陣轉(zhuǎn)置和轉(zhuǎn)換
> m
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> t(m)
[,1] [,2] [,3]
a 1 2 3
b 4 5 6
c 7 8 9
> as.data.frame(m)
a b c
1 1 4 7
2 2 5 8
3 3 6 9

矩陣畫熱圖
> m
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> pheatmap::pheatmap(m)#默認(rèn)是聚類過的

> pheatmap::pheatmap(m,cluster_cols = F,cluster_rows = F)#使行數(shù)和列數(shù)一一對(duì)應(yīng)

列表的新建和取子集
> l <- list(m=matrix(1:9, nrow = 3),
+ df=data.frame(gene = paste0("gene",1:3),
+ sam = paste0("sample",1:3),
+ exp = c(32,34,45)),
+ x=c(1,3,5))
> l
$m
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
$df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
$x
[1] 1 3 5
> l[[2]]
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
> l$df
gene sam exp
1 gene1 sample1 32
2 gene2 sample2 34
3 gene3 sample3 45
抽樣用函數(shù)sample
> sample(1:100,5)#不放回抽樣100個(gè)數(shù)字中抽5個(gè),沒有重復(fù)值,每次運(yùn)行都是隨機(jī),結(jié)果不同
[1] 12 76 19 15 16
> sample(1:10,12,replace = T)#放回抽樣用replace = T,可有重復(fù)值,每次運(yùn)行都是隨機(jī),結(jié)果不同
[1] 10 10 9 9 2 6 9 5 4 3 3 9
若想要每次結(jié)果相同
> set.seed(100)
> sample(1:60,6,replace = T)
[1] 10 55 38 48 51 25
> set.seed(100)
> sample(1:60,6,replace = T)
[1] 10 55 38 48 51 25
> set.seed(100)
> sample(1:60,6,replace = T)
[1] 10 55 38 48 51 25
總結(jié)

練習(xí)3-2
1.統(tǒng)計(jì)iris最后一列有哪幾個(gè)取值,每個(gè)取值重復(fù)了多少次
table(iris$Species)
2.提取iris的前10行,前4列,并轉(zhuǎn)換為矩陣,賦值給a。
a <- as.matrix(iris[1:10,1:4])
3.將a的行名改為flower1,flower2...flower10。
rownames(a) <- paste0("flower",1:nrow(a))
a
4.將a的第4到7行刪除(提示:刪除也是一種修改)
a <- a[-(4:7),]
5.b = rnorm(3),將a和b組成一個(gè)列表,賦值給x
b = rnorm(3)
x <- list(a,b)
x
6.探索x[2]和x[[2]]的區(qū)別(提示:數(shù)據(jù)結(jié)構(gòu))
class(x[2])
class(x[[2]])
8.探索:列表x的元素有名字(names)嗎?
#如果有,它的名字是什么
#如果沒有,試著給它加上名字,隨便取名即可
names(x) #NULL是沒有,不存在的意思。
names(x) <- c("a","b")
補(bǔ)充-元素的名字
元素可命名,用函數(shù)names() ,可根據(jù)名字取子集(向量/數(shù)據(jù)框/列表通用)
> x=1:10
> names(x)=letters[1:10]
> x# 用最簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu)容納兩種不同的信息
a b c d e f g h i j
1 2 3 4 5 6 7 8 9 10
> x["a"]#取x中取名字為a的
a
1
> class(x["a"])
[1] "integer"#整數(shù),依舊為數(shù)值型向量,以X的主題為準(zhǔn),和名字無關(guān)