a<-3
b <- 1
c <- 4
u <- 5+6
rm(b)
rm(u,c)
rm(list = ls())#清空所有变量
x<- c(1,2,3) #常用的向量写法,意为将x定义为由元素1,2,3组成的向量。x
x<- 1:10 #从1-10之间所有的整数x
x<- seq(1,10,by = 0.5) #1-10之间每隔0.5取一个数(注意是逗号不是分号)x
x<- rep(1:3,times=2) #1-3 重复2次x
#这里的x是你刚才赋值的变量名,根据自己的情况来修改
x[4] #x第4个元素
x[-4]#排除法,除了第4个元素之外剩余的元素
x[2:4]#第2到4个元素
x[-(2:4)]#除了第2-4个元素
x[c(1,5)] #第1个和第5个元素
x[x==10]#等于10的元素
x[x<0]
x[x %in% c(1,2,5)]#存在于向量c(1,2,5)中的元素
用以下命令即可获得示例数据框:
X<-read.csv('doudou.txt')
read.table(file=“huahua.txt”, sep=“\t”, header =T)
a <- read.table(file="huahua.txt”, sep”\t”, header =T)
colnames(a) #查看列名
rownames(a) #查看行名,默认值的行名就是行号,1.2.3.4...
dim(a)#几行几列
write.table(a,file = "yu.txt",sep = ",",quote=F)#分隔符改为逗号,字符串不加双引号(默认格式带由双引号)
#这次没有处理完的数据下次想接着用怎么办?--学会保存和重新加载。保存的格式是RData。
save.image(file="bioinfoplanet.RData")#保存当前所有变量
save(a,file="test.RData")#保存其中一个变量
load("test.RData")#再次使用RData时的加载命令
- a[x,y]#第x行第y列
- a[x,]#第x行
- a[,y]#第y列
- a[y] #也是第y列
- a[a:b]#第a列到第b列
- a[c(a,b)]#第a列和第b列
- a$列名#也可以提取列(优秀写法,支持Tab自动补全哦,不过只能提取一列)
plot(iris$Sepal.Length,iris$Sepal.Width)
设置镜像
options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.westlake.edu.cn/bioconductor")
安装包
install.packages()或者BiocManager::install()
install.packages("stringr")
BiocManager::install("limma")
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
install.packages("dplyr")
library(dplyr)
--------
--------
dplyr五个基础函数
mutate(),新增列
mutate(test, new = Sepal.Length * Sepal.Width)
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))
filter()筛选行
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
arrange(),按某1列或某几列对整个表格进行排序
arrange(test, Sepal.Length)#默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小
summarise():汇总
对数据进行汇总操作,结合group_by使用实用性强
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差
# 先按照Species分组,计算每组Sepal.Length的平均值和标准差
group_by(test, Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
dplyr两个实用技能(加载任意一个tidyverse包即可用管道符号)
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
count统计某列的unique值
count(test,Species)
dplyr处理关系数据
inner_join(test1, test2, by = "x") #取交集
left_join(test1, test2, by = 'x') #左连left_join
按照test1
full_join( test1, test2, by = 'x') #取并集
半连接:返回能够与y表匹配的x表所有记录semi_join
semi_join(x = test1, y = test2, by = 'x')
反连接:返回无法与y表匹配的x表的所记录anti_join
anti_join(x = test2, y = test1, by = 'x')
简单合并
在相当于base包里的cbind()函数和rbind()函数;注意,bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数
网页版教程
browseVignettes("limma")
-------------------------------
单细胞
#首先是设置镜像
options("repos"="https://mirrors.ustc.edu.cn/CRAN/")
if(!require("BiocManager")) install.packages("BiocManager",update = F,ask = F)
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
#来自cran的包放在一个向量里
cran_packages <- c('tidyverse',
'msigdbr',
'patchwork',
'SeuratObject',
'Seurat'
)
#来自bioconductor的包放在一个向量里
Biocductor_packages <- c('sva',
'monocle',
'GOplot',
'GSVA',
'plotmo',
'regplot',
'scRNAseq',
'BiocStyle',
'celldex',
'SingleR',
'BiocParallel'
)
#用for循环批量安装来自cran的包
for (pkg in cran_packages){
if (! require(pkg,character.only=T,quietly = T) ) {
install.packages(pkg,ask = F,update = F)
require(pkg,character.only=T)
}
}
#用for循环批量安装来自bioconductor的包
for (pkg in Biocductor_packages){
if (! require(pkg,character.only=T,quietly = T) ) {
BiocManager::install(pkg,ask = F,update = F)
require(pkg,character.only=T)
}
}
#再次加载所有包,检查有没有没安装好的
for (pkg in c(Biocductor_packages,cran_packages)){
require(pkg,character.only=T)
}
#查看Seurat的版本
packageVersion("Seurat")
条件语句,可以根据逻辑值来决定是否执行代码
if(TRUE)print("Hello")
## [1] "Hello"
if(FALSE)print("Hello")
ps = c("tidyr","dplyr","stringr")
for (p in ps) {
print(p)
}
数据库
1.Gene Expression Omnibus (GEO): GEO是一个公共数据库,收集了来自全球研究机构的大量基因表达数据,其中包括很多单细胞测序数据。
2.Single Cell Portal: Single Cell Portal是Broad Institute开发的在线平台,提供了丰富的单细胞测序数据资源和分析工具。https://singlecell.broadinstitute.org/single_cell
3.Human Cell Atlas: 人类细胞图谱计划(Human Cell Atlas)是一个国际合作项目,旨在建立人类所有细胞类型的细胞图谱。他们提供了大量的单细胞 RNA 测序数据。
https://www.humancellatlas.org/
4.Single Cell Expression Atlas: Single Cell Expression Atlas 是由欧洲生物信息研究所(EMBL-EBI)开发的在线数据库。
https://www.ebi.ac.uk/gxa/sc/home
5.UCSC Cell Browser: UCSC Cell Browser 是加州大学圣克鲁兹分校(UCSC)开发的在线平台,用于浏览和分析单细胞RNA测序数据。
https://cells.ucsc.edu/