- 鏡像設(shè)置
Tool-packages 有一個cran鏡像脏毯,但如果要下載bioconductor的包豺总,是沒辦法用的署恍,所以要配置一個可以下載bioconductor的鏡像兴溜。
options("repos"=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
#對應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
#對應(yīng)中科大源
options()$repos #配置成功的話项鬼,查詢結(jié)果為清華鏡像
options()$Bioc_mirror #配置成功的話哑梳,查詢結(jié)果為中科大鏡像
但這種方法下次打開需要重新設(shè)置,所以還需要高級模式的設(shè)置
file.edit('~/.Rprofile')
#啟動編輯文件
options("repos"=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
#添加鏡像绘盟,保存鸠真,退出
重啟查詢options,是對應(yīng)的鏡像
2.安裝
install.packages("包“)
或者
BiocManager::install("包")
#取決于你要安裝的包在哪里
3.加載
library(包)
require(包)
- dplyr五個基礎(chǔ)函數(shù)
4.1 matate(),新增列
mutate(test, new=Sepal.Length*Sepal.Width)
4.2 select(),按列篩選
- 按列號篩選
select(test,1)
#第一列
select(test, c(1,5))
#第一列和第五列
- 按列名篩選
select(test, Sepal.Length)
select(test, Petal.Length, Petal.Width)
或者
vars<- c("Petal.Length","Petal.Width")
select(test, one_of(vars))
#篩選出vars中的向量
4.3 filter(),篩選行
filter(test, Species=="setosa")
filter(test, Species=="setosa"&Sepal.Length>5)
filter(test, Species%in%c("setosa","versicolor"))
#篩選出這兩行
4.4 arrange(),按某1列或某幾列對整個表格進(jìn)行排序
arrange(test,Sepal.Length)
#默認(rèn)從小到大排列
arrange(test, desc(Sepal.Length))
#用desc進(jìn)行從大到小排列
4.5 summarise() 匯總
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
#計算Sepal.Length的平均值和標(biāo)準(zhǔn)差
group_by(test, Species)
summarise(group_by(test, Species), mean(Sepal.Length), sd(Sepal.Length))
#按照Species分組龄毡,計算每組的平均值和標(biāo)準(zhǔn)差
5 dplyr 兩個實用技巧
5.1 管道操作 %>%(cmd/ctr+shift+M)
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length),sd(Sepal.Length))
5.2 count統(tǒng)計某列的unique值
count(test,Species)
- dplyr處理關(guān)系數(shù)據(jù)
options(stringsAsFactors = F)
test1<- data.frame(x=c('b','e','f','x'),
z=c('A','B','C','D'),
stringsAsFactors = F)
test1
test2<- data.frame(x=c('a','b','c','d','e','f'),
y=c(1,2,3,4,5,6),
stringsAsFactors = F)
test2
image.png
image.png
6.1 內(nèi)連
inner_join(test1, test2, by="x")
取交集
6.2 左連
left_join(test1,test2,by='x')
以test1的x為準(zhǔn)
left_join(test2,test2,by='x')
以test2的x為準(zhǔn)
6.3 全連
full_join(test1,test2,by='x')
取并集
6.4 半連接
semi_join(x=test1,y=test2,by='x')
返回能夠與y表匹配的x表中的所有內(nèi)容
image.png
6.5 反連接
anti_join(x=test2,y=test1,by='x')
返回?zé)o法與y表匹配的x表中的所有內(nèi)容
image.png
anti_join(x=test1,y=test2, by='x')
image.png
注意x和y表的先后順序非常重要吠卷,test1,2沦零,順序變了祭隔,結(jié)果完全相反
6.6 簡單合并
test1<-data.frame(x=c(1,2,3,4),y=c(10,20,30,40))
test1
test2<-data.frame(x=c(5,6),y=c(50,60))
test2
test3<-data.frame(z=c(100,200,300,400))
test3
bind_rows(test1,test2)
bind_cols(test1,test3)
image.png
image.png
·