R语言基础学习7
2021-06-23 本文已影响0人
7f0a92cda77c
人见人爱 tidyverse
tidyr
dplyr
stringr
ggplot2

安装好包
options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(!require(tidyr))install.packages("tidyr",update = F,ask = F)
if(!require(dplyr))install.packages("dplyr",update = F,ask = F)
if(!require(stringr))install.packages('stringr',update = F,ask = F)
1 数据清理 tidyr
1.1 tidyr 根据某一列进行合并gather
或者是拆分spread
test <- data.frame(geneid = paste0("gene",1:4),
sample1 = c(1,4,7,10),
sample2 = c(2,5,0.8,11),
sample3 = c(0.3,6,9,12))

gather
操作
test_gather <- gather(data = test,
key = sample_nm,#需要合并到一列的,一般是种类,sample, Species
value = exp,#合并的数值
- geneid)# " - " exclude y with -y
这里的-
代表排除这个变量,不参与到key中

spread
操作-上述的逆操作
test_re <- spread(data = test_gather,
key = sample_nm,
value = exp)

1.2 分割和合并
1.2.1 分割 separate
test_seprate <- separate(test,x, c("X1", "X2"),sep = ",")
X1 X2
1 a b
2 a d
3 b c
1.2.2 合并 unite
unite(test_seprate,"x",X1,X2,sep = "****")
x
1 a****b
2 a****d
3 b****c
1.3 处理NA
X<-data.frame(X1 = LETTERS[1:5],X2 = 1:5)
X[2,2] <- NA
X[4,1] <- NA
# 1.去掉含有NA的行,可以选择只根据某一列来去除
drop_na(X)
drop_na(X,X1)
drop_na(X,X2)
### 2.替换NA
replace_na(X$X2,0)
2 dplyr
初步探索
2.1 五个基础函数 mutate()
select()
filter()
arrange()
summarise()
2.2 两个实用技能 管道操作 %>%
; count统计某列的unique值
2.3 处理关系数据 - 将2个表进行连接
2.1
数据集
library(dplyr)
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL

###1.mutate(),新增列
mutate(test, new = Sepal.Length * Sepal.Width)

#2.select(),按列筛选
####(1)按列号筛选
select(test,1)
select(test,c(1,5))
####(2)按列名筛选
select(test,Sepal.Length)
select(test, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
一组来自tidyselect的有用函数
select(test, starts_with("Petal"))#1
select(test, ends_with("Width"))#2
select(test, contains("etal"))#3
select(test, matches(".t."))#4
select(test, everything())#5
select(test, last_col())#6
select(test, last_col(offset = 1))#7 ##offset---Set it to n to select the nth var from the end.








###3.filter()筛选行
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))

#4.arrange(),按某一列对整个表格进行排序
arrange(test, Sepal.Length)#默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小
arrange(test, desc(Sepal.Width),Sepal.Length)#先是从大到小排的基础上,再按照Length从小到大排列;两个条件

#5.summarise():汇总
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差
#对数据进行汇总操作,结合group_by使用实用性强

2.2 两个实用技能 管道操作 %>%
; count统计某列的unique值
#1:管道操作 %>% (cmd/ctr + shift + M)
library(dplyr)
x1 = filter(iris,Sepal.Width>3)
x2 = select(x1,c("Sepal.Length","Sepal.Width" ))
x3 = arrange(x2,Sepal.Length)
这些可以直接使用管道符传送
iris %>%
filter(Sepal.Width>3) %>%
select(c("Sepal.Length","Sepal.Width" ))%>%
arrange(Sepal.Length)
#2:count统计某列的unique值
count(test,Species)
Species n
1 setosa 2
2 versicolor 2
3 virginica 2
2.3 处理关系数据 - 将2个表进行连接
merge(test1,test2,by="name")#按照列名-name 那一列进行连接的
merge(test1,test3,by.x = "name",by.y = "NAME")

#1.內连inner_join,取交集
inner_join(test1, test2, by = "name")
inner_join(test1,test3,by = c("name"="NAME"))

###2.左连left_join
left_join(test1, test2, by = 'name')
left_join(test2, test1, by = 'name')
###3.全连full_join
full_join(test1, test2, by = 'name')
###4.半连接:返回能够与y表匹配的x表所有记录semi_join
semi_join(x = test1, y = test2, by = 'name')
###5.反连接:返回无法与y表匹配的x表的所记录anti_join
anti_join(x = test2, y = test1, by = 'name')
#6.数据的简单合并
#在相当于base包里的cbind()函数和rbind()函数;注意,bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数

https://www.rstudio.com/resources/cheatsheets/
参照Jimmy团队的生信入门课程,不涉及任何利益冲突