Day6-學(xué)習(xí)R包
安裝和加載R包
鏡像設(shè)置
(引自生信星球教程https://mp.weixin.qq.com/s/XvKb5FjAGM6gYsxTw3tcWw)
自定義CRAN和Bioconductor的下載鏡像,這樣可以加速R包的下載:
# options函數(shù)就是設(shè)置R運(yùn)行過程中的一些選項(xiàng)設(shè)置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對(duì)應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對(duì)應(yīng)中科大源
# 當(dāng)然可以換成其他地區(qū)的鏡像
還可以把這兩行代碼保存成R配置文件,可以實(shí)現(xiàn)在打開Rstudio的時(shí)候自動(dòng)運(yùn)行設(shè)置鏡像。
安裝R包
install.packages(“包”) 安裝CRAN網(wǎng)站上的R包
BiocManager::install(“包”) 安裝Biocductor上的R包
加載R包
library(包)
require(包)
例:安裝和加載dplyr包:
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
install.packages("dplyr")
library(dplyr)

image.png
dplyr基礎(chǔ)應(yīng)用
新增列
mutate()

image.png
按列篩選
select()
按列號(hào)篩選

image.png
按列名篩選

image.png
按行篩選
filter()

image.png
按某列或某幾列對(duì)整個(gè)表格進(jìn)行排序
arrange()

image.png
匯總
summarise()
對(duì)數(shù)據(jù)進(jìn)行匯總操作,結(jié)合group_by使用實(shí)用性強(qiáng)

image.png

image.png

image.png
管道操作
%>%
(cmd/ctrl + shift + M)

image.png
統(tǒng)計(jì)某列的unique值
count()

image.png
dplyr處理關(guān)系數(shù)據(jù)
內(nèi)連,取交集
inner_join()

image.png
左連
left_join()

image.png
全連
full_join()

image.png
半連接
返回能夠與y表匹配的x表所有記錄
semi_join()

image.png
反連接
返回?zé)o法與y表匹配的x表的所有記錄
anti_join()

image.png
簡(jiǎn)單合并
bind_rows()需要兩個(gè)表格列數(shù)相同
bind_cols()需要兩個(gè)數(shù)據(jù)框有相同的行數(shù)
相當(dāng)于base包里的cbind()函數(shù)和rbind()函數(shù)

image.png