【生信技能樹】R語言練習(xí)題 - 初級(1-7題)

題目來源:http://www.bio-info-trainee.com/3793.html

1.打開 Rstudio 告訴我它的工作目錄袜香。
告訴我在你打開的Rstudio里面getwd()代碼運(yùn)行后返回的是什么?

> getwd()
[1] "D:/Bioinformatics/20190413GZ"
  1. 新建6個(gè)向量偶妖,基于不同的原子類型竞思。(重點(diǎn)是字符串树酪,數(shù)值娇斩,邏輯值)
> a1 <- 1:10
> a1
 [1]  1  2  3  4  5  6  7  8  9 10
> class(a1)
[1] "integer"
> a2 <- c('hello','the','world')
> a2
[1] "hello" "the"   "world"
> class(a2)
[1] "character"
> a3 <- c(T,T,T,F,F,T,T,F)
> a3
[1]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE
> class(a3)
[1] "logical"
> a4 <- 607L
> a4
[1] 607
> class(a4)
[1] "integer"
> a5 <- seq(from = 0, to = 20, by =2)
> a5
 [1]  0  2  4  6  8 10 12 14 16 18 20
> class(a5)
[1] "numeric"
> a6 <- c(13.14,14)
> a6
[1] 13.14 14.00
#14也帶小數(shù)位了
> class(a6)
[1] "numeric"
  1. 新建一些數(shù)據(jù)結(jié)構(gòu)茅主,比如矩陣,數(shù)組弃酌,數(shù)據(jù)框绳军,列表等重點(diǎn)是數(shù)據(jù)框,矩陣)在你新建的數(shù)據(jù)框進(jìn)行切片操作矢腻,比如首先取第1,3行射赛, 然后取第4多柑,6列。

參考:R語言數(shù)據(jù)結(jié)構(gòu)

#數(shù)組
> arr <- array(1:8)
> arr
[1] 1 2 3 4 5 6 7 8
#矩陣
> matr <- matrix(1:8,2)
> matr
     [,1] [,2] [,3] [,4]
[1,]    1    3    5    7
[2,]    2    4    6    8
> abc <- paste0('gene',1:5)
> gname <- c('a','b','c','d','e')
> a1 <- c(floor(rnorm(5,5000,300)))
> a2 <- c(floor(rnorm(5,3000,500)))
#數(shù)據(jù)框
> expr <- data.frame(id=abc,genename=gname,s1=a1,s2=a2)
> expr
     id genename   s1   s2
1 gene1        a 4941 3159
2 gene2        b 5297 2845
3 gene3        c 5362 3055
4 gene4        d 4753 2714
5 gene5        e 4806 3361
> dim(expr)
[1] 5 4
> View(expr)
View(expr)
  1. 使用data函數(shù)來加載R內(nèi)置數(shù)據(jù)集 rivers 描述它楣责。并且可以查看更多的R語言內(nèi)置的數(shù)據(jù)集:https://mp.weixin.qq.com/s/dZPbCXccTzuj0KkOL7R31g
> data("rivers") #北美141條河流長度
> rivers
  [1]  735  320  325  392  524  450 1459  135  465  600  330  336  280
 [14]  315  870  906  202  329  290 1000  600  505 1450  840 1243  890
 [27]  350  407  286  280  525  720  390  250  327  230  265  850  210
 [40]  630  260  230  360  730  600  306  390  420  291  710  340  217
 [53]  281  352  259  250  470  680  570  350  300  560  900  625  332
 [66] 2348 1171 3710 2315 2533  780  280  410  460  260  255  431  350
 [79]  760  618  338  981 1306  500  696  605  250  411 1054  735  233
 [92]  435  490  310  460  383  375 1270  545  445 1885  380  300  380
[105]  377  425  276  210  800  420  350  360  538 1100 1205  314  237
[118]  610  360  540 1038  424  310  300  444  301  268  620  215  652
[131]  900  525  246  360  529  500  720  270  430  671 1770
> length(rivers) # 數(shù)據(jù)個(gè)數(shù)
[1] 141
> mean(rivers) # 平均值
[1] 591.1844
> median(rivers) # 中位數(shù)
[1] 425
> max(rivers) # 最大值
[1] 3710
> which.max(rivers) # 最大值下標(biāo)
[1] 68
> min(rivers) # 最小值
[1] 135
> which.min(rivers) # 最小值下標(biāo)
[1] 8
> range(rivers) # 范圍
[1]  135 3710
> sorted_rivers <- sort(rivers) # 排序
> sorted_rivers
  [1]  135  202  210  210  215  217  230  230  233  237  246  250  250
 [14]  250  255  259  260  260  265  268  270  276  280  280  280  281
 [27]  286  290  291  300  300  300  301  306  310  310  314  315  320
 [40]  325  327  329  330  332  336  338  340  350  350  350  350  352
 [53]  360  360  360  360  375  377  380  380  383  390  390  392  407
 [66]  410  411  420  420  424  425  430  431  435  444  445  450  460
 [79]  460  465  470  490  500  500  505  524  525  525  529  538  540
 [92]  545  560  570  600  600  600  605  610  618  620  625  630  652
[105]  671  680  696  710  720  720  730  735  735  760  780  800  840
[118]  850  870  890  900  900  906  981 1000 1038 1054 1100 1171 1205
[131] 1243 1270 1306 1450 1459 1770 1885 2315 2348 2533 3710
> table(rivers) # 統(tǒng)計(jì)
rivers
 135  202  210  215  217  230  233  237  246  250  255  259  260  265 
   1    1    2    1    1    2    1    1    1    3    1    1    2    1 
 268  270  276  280  281  286  290  291  300  301  306  310  314  315 
   1    1    1    3    1    1    1    1    3    1    1    2    1    1 
 320  325  327  329  330  332  336  338  340  350  352  360  375  377 
   1    1    1    1    1    1    1    1    1    4    1    4    1    1 
 380  383  390  392  407  410  411  420  424  425  430  431  435  444 
   2    1    2    1    1    1    1    2    1    1    1    1    1    1 
 445  450  460  465  470  490  500  505  524  525  529  538  540  545 
   1    1    2    1    1    1    2    1    1    2    1    1    1    1 
 560  570  600  605  610  618  620  625  630  652  671  680  696  710 
   1    1    3    1    1    1    1    1    1    1    1    1    1    1 
 720  730  735  760  780  800  840  850  870  890  900  906  981 1000 
   2    1    2    1    1    1    1    1    1    1    2    1    1    1 
1038 1054 1100 1171 1205 1243 1270 1306 1450 1459 1770 1885 2315 2348 
   1    1    1    1    1    1    1    1    1    1    1    1    1    1 
2533 3710 
   1    1 

  1. 下載 https://www.ncbi.nlm.nih.gov/sra?term=SRP133642 里面的 RunInfo Table 文件讀入到R里面竣灌,了解這個(gè)數(shù)據(jù)框,多少列秆麸,每一列都是什么屬性的元素初嘹。(參考B站生信小技巧獲取runinfo table) 這是一個(gè)單細(xì)胞轉(zhuǎn)錄組項(xiàng)目的數(shù)據(jù),共768個(gè)細(xì)胞沮趣,如果你找不到RunInfo Table 文件屯烦,可以點(diǎn)擊下載,然后讀入你的R里面也可以。

5.1 打開鏈接https://www.ncbi.nlm.nih.gov/sra?term=SRP133642驻龟,點(diǎn)擊Send results to Run selector鏈接(圖中紅框)


5.2 點(diǎn)擊RunInfo Table按鈕即可下載RunInfo Table文件(圖中紅圈)

> rm(list = ls())
> options(stringsAsFactors = F)
> sra <- read.table(file = "SraRunTable.txt",header = T,sep = ',')
> dim(sra) # 查看data.frame維度
[1] 768  31
> ncol(sra) #查看data.frame列數(shù)
[1] 31
>View(sra) #用表格形式觀察data.frame
#查看每一列都是什么屬性的元素
> for (i in colnames(sra)) paste(i,class(sra[,i])) %>% print()
Error in paste(i, class(sra[, i])) %>% print() : 
  could not find function "%>%"

報(bào)錯(cuò)了温眉,找不到%>%
google報(bào)錯(cuò),然后就找到了這個(gè)翁狐。
https://stackoverflow.com/questions/30248583/error-could-not-find-function

stackoverflow上的解決方案

ok类溢,復(fù)制粘貼。

> install.packages("magrittr") # only needed the first time you use it
> install.packages("dplyr")    # alternative installation of the %>%
> library(magrittr) # need to run every time you start R and want to use %>%
> library(dplyr)    # alternative, this also loads %>%
> for (i in colnames(sra)) paste(i,class(sra[,i])) %>% print()
[1] "BioSample character"
[1] "Experiment character"
[1] "MBases integer"
[1] "MBytes integer"
[1] "Run character"
[1] "SRA_Sample character"
[1] "Sample_Name character"
[1] "Assay_Type character"
[1] "AssemblyName character"
[1] "AvgSpotLen integer"
[1] "BioProject character"
[1] "Center_Name character"
[1] "Consent character"
[1] "DATASTORE_filetype character"
[1] "DATASTORE_provider character"
[1] "InsertSize integer"
[1] "Instrument character"
[1] "LibraryLayout character"
[1] "LibrarySelection character"
[1] "LibrarySource character"
[1] "LoadDate character"
[1] "Organism character"
[1] "Platform character"
[1] "ReleaseDate character"
[1] "SRA_Study character"
[1] "age character"
[1] "cell_type character"
[1] "marker_genes character"
[1] "source_name character"
[1] "strain character"
[1] "tissue character"

強(qiáng)迫癥患者對于這每行行首的[1]相當(dāng)怨念露懒,我會要解決這個(gè)問題的闯冷。
然后上面那個(gè)數(shù)據(jù)框的切片操作

> #在你新建的數(shù)據(jù)框進(jìn)行切片操作,比如首先取第1懈词,3行蛇耀, 然后取第4,6列
> df_r13 <- sra[c(1,3),]
> dim(df_r13)
[1]  2 31
> View(df_r13)
> df_c46 <- sra[,c(4,6)]
> dim(df_c46)
[1] 768   2
> View(df_c46)
df_r13

df_c46
  1. 下載 https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE111229 里面的樣本信息sample.csv讀入到R里面钦睡,了解這個(gè)數(shù)據(jù)框蒂窒,多少列,每一列都是什么屬性的元素荞怒。(參考 https://mp.weixin.qq.com/s/fbHMNXOdwiQX5BAlci8brA 獲取樣本信息sample.csv)如果你實(shí)在是找不到樣本信息文件sample.csv洒琢,也可以點(diǎn)擊下載

6.1 打開GEO主頁https://www.ncbi.nlm.nih.gov/geo/褐桌,點(diǎn)擊Samples鏈接(圖中紅框)

GEO主頁

6.2 在搜索欄中輸入GSE111229衰抑,點(diǎn)擊Search按鈕,然后點(diǎn)擊Export按鈕

在彈出的對話框點(diǎn)擊Export按鈕荧嵌,得到sample.csv呛踊;

> rm(list = ls())
> options(stringsAsFactors = F)
> geo<- read.csv(file = "sample.csv")
> dim(geo)
[1] 768  12
> ncol(geo)
[1] 12
> View(geo)
> library("magrittr")
> for (i in colnames(geo)) paste(i,class(geo[,i])) %>% print()
[1] "Accession character"
[1] "Title character"
[1] "Sample.Type character"
[1] "Taxonomy character"
[1] "Channels integer"
[1] "Platform character"
[1] "Series character"
[1] "Supplementary.Types character"
[1] "Supplementary.Links character"
[1] "SRA.Accession character"
[1] "Contact character"
[1] "Release.Date character"

  1. 把前面兩個(gè)步驟的兩個(gè)表(RunInfo Table 文件,樣本信息sample.csv)關(guān)聯(lián)起來啦撮,使用merge函數(shù)谭网。
#查看兩個(gè)data.frame里相關(guān)的列
> rm(list = ls())
> options(stringsAsFactors = F)
> sra <- read.table(file = "SraRunTable.txt",header = T,sep = '\t')
> geo<- read.csv(file = "sample.csv")
> library(magrittr) #需要用到 %>%
> for (i in colnames(sra)) {
+   if (i %in% colnames(geo)) print(i)
+ }
[1] "Platform"
#直接查看相同列名,平臺
> sra[1,"Platform"]
[1] "ILLUMINA"
> geo[1,"Platform"]
[1] "GPL13112"
#"平臺"列的內(nèi)容赃春,并不相同愉择。
#考慮在數(shù)據(jù)里查看相同內(nèi)容:
> for (i in colnames(sra)) {
+   for (j in colnames(geo)) {
+     if (sra[,i] %in% geo[,j]) {paste(i,j) %>% print()}
+   }
+ }
[1] "Experiment SRA.Accession"
[1] "Sample_Name Accession"
[1] "Organism Taxonomy"
#以上三組,就是兩個(gè)data.frame內(nèi)容一致的列织中,可以以此為標(biāo)準(zhǔn)合并data.frame
df_merge <- merge(sra,geo,by.x="Experiment",by.y="SRA.Accession")
#df_merge <- merge(sra,geo,by.x="Sample_Name",by.y="Accession")
#df_merge <- merge(sra,geo,by.x="Organism",by.y="Taxonomy")
> dim(df_merge)
[1] 768  42
> dim(sra)
[1] 768  31
> dim(geo)
[1] 768  12
# 42=31+12-1
#其實(shí)锥涕,其中還有兩對("Sample_Name"和"Accession","Organism"和"Taxonomy")內(nèi)容是一致的狭吼,都可以二取其一层坠。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市刁笙,隨后出現(xiàn)的幾起案子破花,更是在濱河造成了極大的恐慌谦趣,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旧乞,死亡現(xiàn)場離奇詭異蔚润,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)尺栖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進(jìn)店門嫡纠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人延赌,你說我怎么就攤上這事除盏。” “怎么了挫以?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵者蠕,是天一觀的道長。 經(jīng)常有香客問我掐松,道長踱侣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任大磺,我火速辦了婚禮抡句,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘杠愧。我一直安慰自己待榔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布流济。 她就那樣靜靜地躺著锐锣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪绳瘟。 梳的紋絲不亂的頭發(fā)上雕憔,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機(jī)與錄音糖声,去河邊找鬼橘茉。 笑死,一個(gè)胖子當(dāng)著我的面吹牛姨丈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播擅腰,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蟋恬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了趁冈?” 一聲冷哼從身側(cè)響起歼争,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤拜马,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后沐绒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體俩莽,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年乔遮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了扮超。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,444評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蹋肮,死狀恐怖出刷,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情坯辩,我是刑警寧澤馁龟,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站漆魔,受9級特大地震影響坷檩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜改抡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一矢炼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雀摘,春花似錦裸删、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至清蚀,卻和暖如春匕荸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背枷邪。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工榛搔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人东揣。 一個(gè)月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓践惑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親嘶卧。 傳聞我的和親對象是個(gè)殘疾皇子尔觉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容