2021-03-07 生信小組 學(xué)習(xí)R包
安裝和加載R包
鏡像設(shè)置
# options函數(shù):設(shè)置R運(yùn)行過程中的一些選項(xiàng)
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)中科大源
安裝
- CRAN的包
install.packages(“ ”)
- Biocductor的包
BiocManager::install(“ ”)
加載
library()
require()
dplyr包常用函數(shù)
- mutate() 新增列
mutate(test, new = Sepal.Length * Sepal.Width)
- select() 按列篩選
#按列號(hào)篩選
select(test,1)
select(test,c(1,5))
select(test,Sepal.Length)
#按列名篩選
select(test, Petal.Length, Petal.Width)
- .filter() 篩選行
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
- arrange() 按某1列或某幾列對(duì)整個(gè)表格進(jìn)行排序
arrange(test, Sepal.Length)#默認(rèn)從小到大排序
arrange(test, desc(Sepal.Length))#用desc從大到小
- summarise() 匯總
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
#group_by(test, Species) 按照Species分組然后計(jì)算每組Sepal.Length的平均值和標(biāo)準(zhǔn)差
dplyr包實(shí)用技能
- 管道操作 %>%
快捷鍵:cmd + option + M
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
- count
統(tǒng)計(jì)某列的unique值
count(test,Species)
dplyr處理關(guān)系數(shù)據(jù)
- 取交集
join
inner_join(test1, test2, by = "x") #內(nèi)連
left_join(test1, test2, by = 'x') #左連
left_join(test2, test1, by = 'x') #右連
full_join( test1, test2, by = 'x') #全連
semi_join(x = test1, y = test2, by = 'x') #半連接:返回能夠與y表匹配的x表所有記錄
anti_join(x = test2, y = test1, by = 'x') #反連接:返回?zé)o法與y表匹配的x表的所記錄
- 簡單合并
bind_rows() #列數(shù)相同
bind_cols() #行數(shù)相同