資料來(lái)源:http://www.bio-info-trainee.com/3793.html
R語(yǔ)言基礎(chǔ)知識(shí)的一些檢驗(yàn)悉尾,最好是對(duì)照幾本R基礎(chǔ)語(yǔ)法書籍來(lái)理解贺嫂。
全部答案及視頻在:https://github.com/jmzeng1314/R_bilibili
# 1.打開(kāi) Rstudio 告訴我它的工作目錄
getwd()
# 2.新建6個(gè)向量栏豺,基于不同的原子類型但惶,(重點(diǎn)是字符串酿矢,數(shù)值动漾,邏輯值)
v1 = c(1, 2, 7, -4, 5) ### "numeric"
v2 = c("Rice", "Wheat") ### "character"
v3 = c(9:17) ### "integer"
v4 = c(1+0i, 2+4i) ### "complex"
v5 = c(TRUE, TRUE, FALSE, TRUE) ### "logical"
v6 = c( "Hello" )
#函數(shù)class()絮缅,查看數(shù)據(jù)類型
# 3.getwd()返回的是當(dāng)前的工作目錄
getwd()
# 4.新建5個(gè)其它數(shù)據(jù)結(jié)構(gòu)鲁沥,矩陣,數(shù)組耕魄,數(shù)據(jù)框画恰,列表,因子(重點(diǎn)是數(shù)據(jù)框吸奴,矩陣)
m1 = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
a1 = array(1:24,dim = c(2,3,4))
df = data.frame(
gender = c("Male", "Male", "Male","Female","Male","Female"),
height = c(152, 171.5, 165, 158, 181.5, 175),
weight = c(81,93,78,45,32,65),
Age = c(42,38,26,45,32,65))
newlist = list(v1,v2,v3,v4,v5)
newfactor <- factor(letters[1:20], labels = "letter")
# 5.在你新建的數(shù)據(jù)框進(jìn)行切片操作允扇,比如首先取第1,3行则奥, 然后取第2考润,4列
df[c(1,3),]
df[,c(2,4)]
# 6.使用data函數(shù)來(lái)加載R內(nèi)置數(shù)據(jù)集 rivers 描述它。
data('rivers')
rivers
# 7.下載 https://www.ncbi.nlm.nih.gov/sra?term=SRP133642 里面的 RunInfo Table 文件讀入到R里面读处,
#了解這個(gè)數(shù)據(jù)框糊治,多少列,每一列都是什么屬性的元素罚舱。
RunInfoTab <- read.csv('SraRunTable.txt',row.names = 1,sep = '\t')
dim(RunInfoTab)
nrow(RunInfoTab)
ncol(RunInfoTab)
str(RunInfoTab)
# 8.下載 https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE111229 里面的樣本信息
#讀入到R里面井辜,了解這個(gè)數(shù)據(jù)框揖赴,多少列,每一列都是什么屬性的元素抑胎。
#方法1:
options(stringsAsFactors = F)
GEO=read.csv('sample.csv')
nrow(GEO)
ncol(GEO)
class(GEO)
str(GEO)
#方法2:
# 函數(shù)getGEO的下載方法
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
BiocManager::install('GEOquery')
library(GEOquery) #加載GEOquery這個(gè)包
GEOdata<-getGEO('GSE111229',getGPL = F)
#使用getGEO函數(shù)下載數(shù)據(jù),GPL文件注釋一般比較大,不下載
save(GEOdata,file='GEOdata.Rdata')
load("GEOdata.Rdata") #導(dǎo)入數(shù)據(jù)
pdata<- pData(GEOdata[[1]])
#pData()函數(shù)可以調(diào)用實(shí)驗(yàn)設(shè)計(jì)的相關(guān)內(nèi)容
class(pdata)
dim(pdata)
colnames(pdata) #geo_accession就是樣品名
# 9.把兩個(gè)表關(guān)聯(lián)起來(lái)渐北,使用merge函數(shù)阿逃。
?merge()
###by.x = 'Sample_Name',by.y = 'Accession'這里的Sample_Name與Accession必須代表同樣的數(shù)據(jù)!
identical(RunInfoTab$Sample_Name , GEO$Accession) #此處2列的行數(shù)不一致赃蛛,但內(nèi)容一致
d=merge(RunInfoTab,GEO,by.x = 'Sample_Name',by.y = 'Accession')
e=d[,c("MBases","Title")]
save(e,file = 'input.Rdata')
# 10.對(duì)前面讀取的 RunInfo Table 文件在R里面探索其MBases列恃锉,
#包括 箱線圖(boxplot)和五分位數(shù)(fivenum),還有頻數(shù)圖(hist)呕臂,以及密度圖(density) 破托。
rm(list = ls())
options(stringsAsFactors = F)
load(file = 'input.Rdata')
View(e)
boxplot(e$MBases)
fivenum(e$MBases)
hist(e$MBases)
plot(density(e$MBases))
# 11.把前面讀取的樣本信息表格的樣本名字根據(jù)下劃線分割看第3列元素的統(tǒng)計(jì)情況。
#第三列代表該樣本所在的plate
e[,2]
plate=unlist(lapply(e[,2],function(x){
# x=e[1,2]
x
strsplit(x,'_')[[1]][3] #中括號(hào)的理解
}))
table(plate)
###plate
#0048 0049
#384 384
# 12.根據(jù)plate把關(guān)聯(lián)到的 RunInfo Table 信息的MBases列分組檢驗(yàn)是否有統(tǒng)計(jì)學(xué)顯著的差異歧蒋。
e$plate=plate
t.test(e[,1]~plate)
#data: e[, 1] by plate
#t = 2.3019, df = 728.18, p-value = 0.02162
# 13.分組繪制箱線圖(boxplot)土砂,頻數(shù)圖(hist),以及密度圖(density) 谜洽。
boxplot(e[,1]~plate)
# 14.使用ggplot2把上面的圖進(jìn)行重新繪制萝映。
library(ggplot2)
colnames(e)
ggplot(e,aes(x=plate,y=MBases)) +
geom_boxplot() #boxplot
e$plate=factor(e$plate) #將plate轉(zhuǎn)換為因子類型
ggplot(e,aes(x=MBases))+
geom_histogram(fill='pink',colour='blue')+
facet_grid(. ~plate) #hist
ggplot(e,aes(x=plate,y=MBases))+
geom_point() +
stat_density2d(aes(alpha=..density..),geom="raster",contour=F) #density
# 15.使用ggpubr把上面的圖進(jìn)行重新繪制。
library(ggpubr)
p <- ggboxplot(e, x = "plate", y = "MBases",
color = "plate", palette = "jco",
add = "jitter")
p+stat_compare_means(method = 't.test')
# 16.隨機(jī)取384個(gè)MBases信息阐虚,跟前面的兩個(gè)plate的信息組合成新的數(shù)據(jù)框序臂,第一列是分組,第二列是MBases实束,總共是384*3行數(shù)據(jù)奥秆。
newdat<- sample (e$MBases, 384, replace = FALSE)
data1 <- e[newdat, ]
data2 <- data1[,c(3,1,2)]
dim(data2)
感謝Jimmy老師,以及生信技能樹(shù)團(tuán)隊(duì)的資源!!!