今天主要学习一个dplyr
包
老规矩,走流程
step 1 安装包
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
if(!require(dplyr))install.packages("dplyr")
library(dplyr)
step2 认识五个基础函数
1
mutate()
新增列
test <- iris[c(1:2,51:52,101:102),]
mutate(test, new = Sepal.Length * Sepal.Width)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
# 1 5.1 3.5 1.4 0.2 setosa 17.85
# 2 4.9 3.0 1.4 0.2 setosa 14.70
# 3 7.0 3.2 4.7 1.4 versicolor 22.40
# 4 6.4 3.2 4.5 1.5 versicolor 20.48
# 5 6.3 3.3 6.0 2.5 virginica 20.79
# 6 5.8 2.7 5.1 1.9 virginica 15.66
2
select()
筛选列
#1 根据列名
select(test,Sepal.Length,Sepal.Width)
# Sepal.Length Sepal.Width
# 1 5.1 3.5
# 2 4.9 3.0
# 51 7.0 3.2
# 52 6.4 3.2
# 101 6.3 3.3
# 102 5.8 2.7
#2 根据列号
select(test,c(1,4))
# Sepal.Length Petal.Width
# 1 5.1 0.2
# 2 4.9 0.2
# 51 7.0 1.4
# 52 6.4 1.5
# 101 6.3 2.5
# 102 5.8 1.9
a <- c("Sepal.Length","Sepal.Width")
#3 根据变量
select(test,one_of(a))
# Sepal.Length Sepal.Width
# 1 5.1 3.5
# 2 4.9 3.0
# 51 7.0 3.2
# 52 6.4 3.2
# 101 6.3 3.3
# 102 5.8 2.7
3
filter()
筛选行
filter(test,Species=="versicolor")
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 7.0 3.2 4.7 1.4 versicolor
# 2 6.4 3.2 4.5 1.5 versicolor
filter(test, Species == "versicolor"&Sepal.Length > 5 )
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 7.0 3.2 4.7 1.4 versicolor
# 2 6.4 3.2 4.5 1.5 versicolor
filter(test, Species %in% c("virginica","versicolor"))
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 7.0 3.2 4.7 1.4 versicolor
# 2 6.4 3.2 4.5 1.5 versicolor
# 3 6.3 3.3 6.0 2.5 virginica
# 4 5.8 2.7 5.1 1.9 virginica
4
arrange()
按列排序
arrange(test,Sepal.Length)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 4.9 3.0 1.4 0.2 setosa
# 2 5.1 3.5 1.4 0.2 setosa
# 3 5.8 2.7 5.1 1.9 virginica
# 4 6.3 3.3 6.0 2.5 virginica
# 5 6.4 3.2 4.5 1.5 versicolor
# 6 7.0 3.2 4.7 1.4 versicolor
# des()从大到小
arrange(test,desc(Sepal.Length),Sepal.Width )
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 7.0 3.2 4.7 1.4 versicolor
# 2 6.4 3.2 4.5 1.5 versicolor
# 3 6.3 3.3 6.0 2.5 virginica
# 4 5.8 2.7 5.1 1.9 virginica
# 5 5.1 3.5 1.4 0.2 setosa
# 6 4.9 3.0 1.4 0.2 setosa
5
summarise()
按列排序
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
# mean(Sepal.Length) sd(Sepal.Length)
# 1 5.916667 0.8084965
summarise(group_by(test,Species),max(Sepal.Length), var(Sepal.Length))
# Species `max(Sepal.Length)` `var(Sepal.Length)`
# <fct> <dbl> <dbl>
# 1 setosa 5.1 0.0200
# 2 versicolor 7 0.180
# 3 virginica 6.3 0.125
step3 实用技能一:管道符
%>% (cmd/ctr + shift + M)
test %>% group_by(Species) %>% summarise(mean(Sepal.Length),sd(Sepal.Length))
# Species `mean(Sepal.Length)` `sd(Sepal.Length)`
# <fct> <dbl> <dbl>
# 1 setosa 5 0.141
# 2 versicolor 6.7 0.424
# 3 virginica 6.05 0.354
实用技能二:
count()
统计m某一每个元素的值
count(test,Species)
# Species n
# <fct> <int>
# 1 setosa 2
# 2 versicolor 2
# 3 virginica 2
step4 处理关系数据
1 内连
options(stringsAsFactors = F)
test1 <- data.frame(symbol=paste0("gene",1:3),exp=c(1,2,3))
test1
# symbol exp
# 1 gene1 1
# 2 gene2 2
# 3 gene3 3
test2 <- data.frame(symbol=paste0("gene",1:5),value=rnorm(5))
test2
# symbol value
# 1 gene1 -0.9241406
# 2 gene2 -1.8783760
# 3 gene3 0.7187198
# 4 gene4 0.6577491
# 5 gene5 -0.3685181
inner_join(test1,test2,by="symbol")
# symbol exp value
# 1 gene1 1 2.3987799
# 2 gene2 2 0.1082610
# 3 gene3 3 -0.9636356
2 左连
left_join(test1,test2,by="symbol")
# symbol exp value
# 1 gene1 1 2.3987799
# 2 gene2 2 0.1082610
# 3 gene3 3 -0.9636356
left_join(test2,test1,by="symbol")
# symbol value exp
# 1 gene1 2.3987799 1
# 2 gene2 0.1082610 2
# 3 gene3 -0.9636356 3
# 4 gene4 0.4143826 NA
# 5 gene5 0.6006878 NA
3 全连
full_join(test1,test2,by="symbol")
# symbol exp value
# 1 gene1 1 2.3987799
# 2 gene2 2 0.1082610
# 3 gene3 3 -0.9636356
# 4 gene4 NA 0.4143826
# 5 gene5 NA 0.6006878
4 半连 在test1中找包含test2的内容
semi_join(test1,test2,by="symbol")
# symbol exp
# 1 gene1 1
# 2 gene2 2
# 3 gene3 3
5 反连 在test2中找不包含test1的内容
anti_join(x=test2,y=test1,by="symbol")
# symbol value
# 1 gene4 0.4143826
# 2 gene5 0.6006878
6 简单合并
test3 <- data.frame(x=letters[1:6],y=LETTERS[15:20])
bind_cols(test,test3)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species x y
# 1 5.1 3.5 1.4 0.2 setosa a O
# 2 4.9 3.0 1.4 0.2 setosa b P
# 3 7.0 3.2 4.7 1.4 versicolor c Q
# 4 6.4 3.2 4.5 1.5 versicolor d R
# 5 6.3 3.3 6.0 2.5 virginica e S
# 6 5.8 2.7 5.1 1.9 virginica f T
bind_rows(test1,test2)
# symbol exp value
# 1 gene1 1 NA
# 2 gene2 2 NA
# 3 gene3 3 NA
# 4 gene1 NA 2.3987799
# 5 gene2 NA 0.1082610
# 6 gene3 NA -0.9636356
# 7 gene4 NA 0.4143826
# 8 gene5 NA 0.6006878