補充[ZHUHAI_Biotrainee] 第一周_課堂總結(jié)
內(nèi)容:http://www.reibang.com/p/bbe94addb8ba
一、R及RStudio介紹
1读串、R的數(shù)據(jù)類型:
A拇舀、以下三類:
數(shù)值型
字符型
邏輯型
B喂急、判斷數(shù)據(jù)類型:
class()
is.numeric/is.character/is.logical
2菩咨、數(shù)據(jù)結(jié)構(gòu)
一維:向量
二維:矩陣丢烘、數(shù)據(jù)框俱箱、list国瓮;與一維相同,都必須含有一個數(shù)據(jù)類型狞谱;
3乃摹、List
總覽概況:
列表
↓
元素
↓
向量、矩陣跟衅、數(shù)據(jù)框孵睬、list
索引也是這樣層層遞進(jìn)的關(guān)系
二、腳本1:script1_vector_creat
1伶跷、rep()函數(shù)掰读,區(qū)分其中的參數(shù),each叭莫、times
Eg1:times
> ve2<-1:3
> ve2
[1] 1 2 3
> ve4<-rep(x =ve2,times=3)####常用的創(chuàng)建向量的函數(shù)3 rep
> ve4
[1] 1 2 3 1 2 3 1 2 3
對ve4中的所有元素按照邏輯循環(huán)三次
Eg2:each
> a <- c(1,2,3)
> a
[1] 1 2 3
> rep(a,c(3,4,5))
[1] 1 1 1 2 2 2 2 3 3 3 3 3
依次對a中的單個元素進(jìn)行單獨設(shè)置循環(huán)數(shù)
2蹈集、區(qū)分paste()以及paste0()
paste:默認(rèn)的分割符(空格)
paste0:默認(rèn)無分隔符
Egpaste
:
> a<-c('I')
> b<-c('like')
> f<-c('I','You','She','He')
> tmp<-c('Chandeler','Ross','Rachel','Monica','Joey','Pheebe')
> paste(a,b,tmp)
[1] "I like Chandeler" "I like Ross" "I like Rachel" "I like Monica" "I like Joey"
[6] "I like Pheebe"
paste有分隔符,如果有分隔符雇初,可以添加sep = ''
> paste(a,b,tmp,sep = '')
[1] "IlikeChandeler" "IlikeRoss" "IlikeRachel" "IlikeMonica" "IlikeJoey" "IlikePheebe"
Egpaste0
:
> paste0(a,b,tmp)
[1] "IlikeChandeler" "IlikeRoss" "IlikeRachel" "IlikeMonica" "IlikeJoey" "IlikePheebe"
與上一個例子> paste(a,b,tmp,sep = '')
3拢肆、length()查看元素個數(shù)
三、腳本3:script3_vector_math
1靖诗、運算法則
%%:取余數(shù) (注意:%%取余數(shù)用的時候較多)
%/%:取整數(shù)
!= 判斷是否不完全相等郭怪,因為有!
== ①判斷是否完全相等呻畸;②返回的結(jié)果為邏輯值
與或非
實戰(zhàn)%%
題目如下:
a<-seq(1,10,1)移盆;找出a中的奇數(shù)(練習(xí)使用操作符)
> a<-seq(1,10,1)
> a
[1] 1 2 3 4 5 6 7 8 9 10
> a%%2
[1] 1 0 1 0 1 0 1 0 1 0
> a[a%%2==1]
[1] 1 3 5 7 9
四、腳本4:script4_matrix
1伤为、取出奇偶行咒循、利用邏輯值(T据途、F)/(1、0)
實戰(zhàn)題目:
1.獲取e的奇數(shù)行內(nèi)容
2.獲取e的偶數(shù)行內(nèi)容
> e<-LETTERS[1:24]
> e
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X"
> dim(e)<-c(6,4)
> e
[,1] [,2] [,3] [,4]
[1,] "A" "G" "M" "S"
[2,] "B" "H" "N" "T"
[3,] "C" "I" "O" "U"
[4,] "D" "J" "P" "V"
[5,] "E" "K" "Q" "W"
[6,] "F" "L" "R" "X"
> e_t<-1:nrow(e)
> e[e_t%%2==1,]
[,1] [,2] [,3] [,4]
[1,] "A" "G" "M" "S"
[2,] "C" "I" "O" "U"
[3,] "E" "K" "Q" "W"
> a[a%%2==1]
[1] 1 3 5 7 9
> e[e_t%%2==0,]
[,1] [,2] [,3] [,4]
[1,] "B" "H" "N" "T"
[2,] "D" "J" "P" "V"
[3,] "F" "L" "R" "X
思路:
①明確e是矩陣叙甸,dim()
②對1:nrow()
中才可以選擇奇數(shù)偶數(shù)行
③a%%2
的結(jié)果為1颖医,0 根據(jù)1,0而選擇奇數(shù)偶數(shù)行
2裆蒸、head(),tail()函數(shù)
可以對其中的參數(shù)n進(jìn)行設(shè)置熔萧;head()取首;tail()取尾
五僚祷、腳本5:script5_dataframe
1佛致、對數(shù)據(jù)框的索引用[](位置、名字辙谜、邏輯值)俺榆,另外還可以用$(matrix,向量不可以用)
> date<-c(21,22,23,35,52)
> plan<-c('mon','tue','wed','thur','fri')
> color<-c('green','red','white','black','purple')
> April<-data.frame(date,plan,color)
> April
date plan color
1 21 mon green
2 22 tue red
3 23 wed white
4 35 thur black
5 52 fri purple
> April$date[2]
[1] 22
實戰(zhàn):索引出April數(shù)據(jù)框的第2到4行第3列的元素(兩種方法)
> April[2:4,3]
[1] "red" "white" "black"
> April[2:4,"color"]
[1] "red" "white" "black"
> April[c(F,T,T,T,T,F),"color"]
[1] "red" "white" "black" "purple"
> April[c(F,T,T,T,T),"color"]
[1] "red" "white" "black" "purple"
六装哆、腳本6:script6_list
1罐脊、str()函數(shù),查看數(shù)據(jù)結(jié)構(gòu)
Eg:
> str(r_list)
List of 4
$ flag: chr "Hello world"
$ yes : int [1:5] 1 2 3 4 5
$ : int [1:5, 1:2] 1 2 3 4 5 6 7 8 9 10
$ : chr [1:3] "mon" "tue" "wed"
七蜕琴、腳本7:script7_read_a_file
1萍桌、讀入文件:read.csv/table
①讀進(jìn)來存儲數(shù)據(jù)格式的都是data.frame;
②reead.csv默認(rèn)分隔符為逗號,read.table默認(rèn)分隔符為一個或者多個空格凌简、制表符上炎、換行符、回車
③row.names = 1 將第一列作為行名
存成文件:'0418.txt'中后綴是告訴用什么文件打開雏搂;
④comment.char = '!'
去掉反症!部分 注釋符號
常見:
d<-read.csv('GSE17215_series_matrix.txt.gz',
comment.char = '!',sep='\t',row.names = 1)
f<-read.table('GSE17215_series_matrix.txt.gz',
comment.char = '!',
header=T,
row.names = 1)
2、輸出文件:write.table/write.csv
編碼文件:write.table/write.csv
用什么編碼格式編碼文件畔派,從而對應(yīng)不同的保存格式,csv
代表用對csv
編碼文件润绵,table
代表txt
編碼文件
存成文件:'0418.txt'
中后綴是告訴用什么文件打開线椰,若‘0908.csv’
代表用csv格式打開:
Eg:
write.table(x = f,file = '0418.txt')
save(f,file = '17215.Rdata')
save對應(yīng)Rdata等形式的保存文件;table對應(yīng)txt尘盼;csv對應(yīng)csv憨愉;
八、packages
1卿捎、思路:
代碼+說明文檔+數(shù)據(jù)集
刪除包:remove.packages()
2配紫、若一次性安裝多個R包,可使用如下代碼:
#library("monocle")
BiocManager::install("slingshot")
options(BioC_mirror="http://mirrors.ustc.edu.cn/bioc/")
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options()$repos
options()$BioC_mirror
shanshan <- c("scatterplot3d",
"made4",
"pheatmap",
"matrixStats",
"statmod",
"FactoMineR",
"jackstraw",
"ReactomePA",
"org.Mm.eg.db",
"clusterProfiler",
"GOSemSim",
"arulesViz",
"ggpubr")
packages=(shanshan
)
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
BiocManager::install(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
ipak(packages)
library("taRifx")
library("matrixStats")
library("ggplot2")
library("Rtsne")
修改包名午阵,注意檢查R包是否安裝成功躺孝。
九享扔、腳本9:script9_for_apply_function
1、for()函數(shù)
①for函數(shù)的格式:for(循環(huán)數(shù)){}
,其中x代表了for循環(huán)數(shù)數(shù)植袍,{}是要執(zhí)行的操作
例:for(i in 1:nrow(test)){test_r[i,1]<-mean(c(test[i,1],test[i,3]))test_r[i,2]<-paste0(test[i,2],test[i,4]) }
②使用for函數(shù)時惧眠,要對for的輸出結(jié)果有了解
實戰(zhàn):
實現(xiàn)每行的第1個和第3個元素取均值,第2個和第4個元素?zé)o縫連接在一起
> test<-data.frame(a=seq(1,10,2),
+ b=seq(2,11,2),
+ c=seq(11,20,2),
+ d=c('mon','tues','wednes','thurs','fri'))
> test
a b c d
1 1 2 11 mon
2 3 4 13 tues
3 5 6 15 wednes
4 7 8 17 thurs
5 9 10 19 fri
> test_r<-data.frame()
> for(i in 1:nrow(test)){
+ test_r[i,1]<-mean(c(test[i,1],test[i,3]))
+ test_r[i,2]<-paste0(test[i,2],test[i,4])
+ }
> test_r
V1 V2
1 6 2mon
2 8 4tues
3 10 6wednes
4 12 8thurs
5 14 10fri
思路:
①根據(jù)題目要求于个,首先明確輸出的結(jié)果為data.frame
②設(shè)置空載容器氛魁,用來裝輸出結(jié)果,所以設(shè)置空向量test_r<-data.frame()
②for函數(shù)的()里:是對每一行的元素進(jìn)行操作厅篓,所以設(shè)置參數(shù)i
秀存,并且為i:nrow(test)
,對每一行循環(huán)操作
③for函數(shù)的{}里:根據(jù)題意羽氮,取出元素并輸入相應(yīng)函數(shù)mean(),paste0()
2或链、funcation()函數(shù)以及apply()函數(shù)
往往apply()函數(shù)不能單獨使用,需要結(jié)合funcation()函數(shù)
funcation()函數(shù)
:
①funcation()函數(shù)的格式為funcation(x){}
乏苦,x為設(shè)置的參數(shù)株扛,{}里面是對參數(shù)執(zhí)行的相應(yīng)代碼
實戰(zhàn):
用function和apply求出test里每行1、2汇荐、3個元素的標(biāo)準(zhǔn)差和平均值洞就,并第4個元素后添加'day';
> test<-data.frame(a=seq(1,10,2),
+ b=seq(2,11,2),
+ c=seq(11,20,2),
+ d=c('mon','tues','wednes','thurs','fri'))
> tes
>y<-function(x){
mean_row<-mean(as.numeric(c(x[1],x[2],x[3])))
sd_row <- sd(as.numeric(c(x[1],x[2],x[3])))
str_paste<-paste0(x[4],"day")
result<-c(mean_row,str_paste,str_paste)
return(result)
}
>test1<-apply(test,1,y)
test1
注意:
①funcation中x為參數(shù),是定義該函數(shù)的一個參數(shù)掀淘。而對每一行進(jìn)行操作旬蟋,是在apply中的1
表現(xiàn)出來的;
②在定義funcation的參數(shù)時革娄,我們要斟酌好x代表的含義倾贰,根據(jù)題目要求,是對每一行進(jìn)行操作操作拦惋,這里x代表每行匆浙;所以在funcation{}里對參數(shù)執(zhí)行代碼時,應(yīng)該表示出每一行的第幾個元素厕妖。