R語言極簡(jiǎn)入門

一、R與Rstudio的安裝

· R可以在CRAN上免費(fèi)下載:(https://www.r-project.org/)

· ①. 首先,點(diǎn)擊 download R

image.png

· ②. 其次,選擇清華鏡像

image.png

· ③. 接著,選擇合適自己系統(tǒng)的R版本

image.png

· ④. 最后,選擇base,即可安裝成功
image.png

· 然后,安裝RStudio:(https://rstudio.com/products/rstudio/download/)
· RStudio是比較推薦的R的集成開發(fā)軟件
· 我們安裝RStudio的免費(fèi)版本

image.png

· 同樣要選擇合適自己系統(tǒng)的版本
image.png

· 最后,假如你是window,建議你再安裝一個(gè)Rtools
· Rtools的作用是什么?就是為了在window系統(tǒng)下更好地建設(shè)R包環(huán)境
· 例如,你在window下安裝R包shiny時(shí),沒有安裝Rtools就會(huì)報(bào)錯(cuò)
· 在清華大學(xué)的 CRAN 鏡像中可以下載 Rtools
image.png

image.png

· 假如你的R不是最新的,你也可以在this page頁(yè)面下載到和自己R版本對(duì)應(yīng)的Rtools工具

image.png

image.png

· 安裝 Rtools時(shí),一定要要勾選添加 rtools 到環(huán)境變量中這個(gè)選項(xiàng)
image.png

· 安裝Rtools的第二種方法:通過設(shè)置清華大學(xué) CRAN 后,先安裝installr包,再通過這個(gè)包直接下載安裝 Rtools

options("repos"=c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
install.packages('installr')
library(installr)
install.Rtools()

二、R包的安裝

①. 從CRAN中安裝R包

· 我們可以在控制臺(tái)直接輸入"install.packages("ggplot2")"


image.png

· 我們也可以通過RStudio安裝“ggplot2”


image.png

重點(diǎn)掌握:
· R包安裝成功后,我們通過library(ggplot2)require(ggplot2)加載到環(huán)境中;
· 假如你想查看已安裝的R包,可以使用installed.packages()查看;
· 假如你想知道R包被安裝在哪個(gè)位置,可以使用.libPaths();
· 假如你想將已加載的R包從環(huán)境中卸載,可以使用detach("package:ggplot2"),也可以通過"Packages"界面取消勾選相應(yīng)的R包;
· 假如你查看R包里面有什么函數(shù),可以通過help(package="ggplot2");
· 假如你對(duì)ggplot2中的某個(gè)函數(shù)不太清楚,例如geom_bar函數(shù),可以通過?geom_bar查看它的具體用法
· 假如你想移除你的R包,可以使用remove.packages("ggplot2")
· 假如你想查看R版本及加載進(jìn)去的R包的信息,可以使用sessionInfo()

②.從bioconductor中安裝R包

image.png

· 從bioconductor中安裝R包,要先安裝BiocManager

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("clusterProfiler")

· 如果感覺從CRAN或者Bioconductor上下載太慢,可以適當(dāng)改變一下鏡像

############################### r語言最新鏡像 ##############################
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
options("repos" = c(CRAN="http://mirrors.cloud.tencent.com/CRAN/")) 
options(download.file.method = 'libcurl')
options(url.method='libcurl')
BiocManager::install("miRNAtap",ask = F,update = F)
BiocManager::install("topGO",ask = F,update = F)
BiocManager::install("miRNAtap.db",ask = F,update = F)

③.從github中安裝R包

image.png

· 從github中安裝R包,要先安裝devtools

install.packages("devtools")
library(devtools)
devtools::install_github("jokergoo/ComplexHeatmap")

· 如果有時(shí)候網(wǎng)速太慢,迫不得已需要下載zip包(推薦用迅雷

image.png

· 從github上將zip包下載到本地后,我們需要經(jīng)過編譯才能安裝上;
· 編譯的方式有兩種:
· ①. devtools::install_local("路徑")

devtools::install_local("C:\\Users\\Administrator\\Desktop\\ComplexHeatmap.zip")
devtools::install_local("C:/Users/Administrator/Desktop/ComplexHeatmap.zip")

· ②. 先解壓zip成文件夾,install.packages("路徑",repos = NULL, type="source")

install.packages("C:\\Users\\Administrator\\Desktop\ComplexHeatmap",repos = NULL, type="source")

· 這兩種編譯方式看起來一樣,但實(shí)則大不一樣。
· 前者的路徑寫到zip文件即可,后者需要先把zip文件加壓后,然后再路徑寫到文件夾;
· 在window系統(tǒng)中,路徑要用\\或者 /隔開,在linux中要用\

④.從gitee中安裝R包

· 如果有時(shí)候github上網(wǎng)速慢到zip包都下載不下來,或者R包項(xiàng)目文件非常大、非常多難以下載,這時(shí)候我們就需要先把github上的包克隆到gitee,然后再?gòu)?code>gitee上下載
· 因?yàn)?code>gitee在國(guó)內(nèi)的下載速度還是很喜人的
· 假如你已經(jīng)把項(xiàng)目克隆到gitee上,你可以直接從gitee上安裝R包

devtools::install_git("https://gitee.com/jmzeng/GEOmirror")

· 如果你要直接install_git,并且你的電腦是window系統(tǒng),記得提前安裝一個(gè)Git客戶端,給電腦增加一個(gè)git環(huán)境,才能運(yùn)行成功這個(gè)代碼。
.當(dāng)然,你也可以從gitee上下載zip文件,然后再在本地進(jìn)行編譯安裝
· gitee的順利使用,需要你先在gitee上注冊(cè)賬號(hào)。

三、R語言編程基礎(chǔ)

· 查看當(dāng)前工作目錄,可以用getwd()函數(shù)。
· 設(shè)置工作目錄,可以用setwd()函數(shù),也可以在Rstudio里面更改

> getwd()
[1] "/home/shpc_8b2a385294/conf"
> setwd("~/fcq")
> getwd()
[1] "/home/shpc_8b2a385294/fcq"

· R的注釋有#開頭,出現(xiàn)在#后面的文本會(huì)被R解釋器忽略
· R語句由函數(shù)和賦值構(gòu)成,賦值符號(hào)為<-=,雖然這兩個(gè)都能起到賦值的作用,但個(gè)人建議用<-,這樣不容易出錯(cuò)
· <-的快捷鍵是Alt+-

> a <- 2 # I'm an anotataion
> a
[1] 2
> a = 2 # I'm an anotataion
> a
[1] 2
> 

· R語言的數(shù)學(xué)運(yùn)算和我們平時(shí)的數(shù)學(xué)運(yùn)算(+、-、*、/)是一致的

> 1+1
[1] 2
> 1-1
[1] 0
> 1*1
[1] 1
> 1/1
[1] 1
> a = 1+1
> a
[1] 2

· class()函數(shù)可以查看數(shù)據(jù)類型、數(shù)據(jù)結(jié)構(gòu)
· R中有4種數(shù)據(jù)類型:數(shù)值型、字符型、邏輯型日期型

> a <- 1
> class(a)
[1] "numeric"
> a <- "1"
> class(a)
[1] "character"
> a <- T
> a
[1] TRUE
> class(a)
[1] "logical"
> a <- TRUE
> a
[1] TRUE
> class(a)
[1] "logical"
> a <- as.Date("2021-03-17")
> a
[1] "2021-03-17"
> class(a)
[1] "Date"

· 字符不能直接相加,但數(shù)值可以直接相加

> a <- "1"
> b <- "1"
> a
[1] "1"
> b
[1] "1"
> a+b
Error in a + b : non-numeric argument to binary operator
> a <- 1
> b <- 1
> a+b
[1] 2

· R語言的邏輯運(yùn)算:>、==、<、>= 、<=、!=、&(和)|(或)、!(非)

> a <- 1
> a > 1
[1] FALSE
> a = 1
> a == 1
[1] TRUE
> a < 1
[1] FALSE
> a >= 1
[1] TRUE
> a <= 1
[1] TRUE
> a != 1
[1] FALSE
> a <- T
> a
[1] TRUE
> b <- F
> b
[1] FALSE
> a & b
[1] FALSE
> a | b
[1] TRUE
> !a
[1] FALSE
> !b
[1] TRUE

· R語言的數(shù)據(jù)結(jié)構(gòu):向量、矩陣、數(shù)據(jù)框、列表
· 1. 向量:使用c()函數(shù)創(chuàng)建,常見有3種類型:數(shù)值型、字符型邏輯型。單個(gè)向量的數(shù)據(jù)類型是固定的。

> a <- c(1,2,3,4)
> a
[1] 1 2 3 4
> class(a)
[1] "numeric"
> a <- c("1","2","3","4")
> a
[1] "1" "2" "3" "4"
> class(a)
[1] "character"
> a <- c(T,F,T,F)
> a
[1]  TRUE FALSE  TRUE FALSE
> class(a)
[1] "logical"
> a <- c(1,2,"3","4")
> a
[1] "1" "2" "3" "4"
> class(a)
> a <- c(1,2,T,F)
> a
[1] 1 2 1 0

· ①. 向量的索引

> a <- c(1,2,4,3)
> a
[1] 1 2 4 3
> a[1]
[1] 1
> a[3]
[1] 4
> a[1:3]
[1] 1 2 4
> a[-1]
[1] 2 4 3
> a[c(1,4)]
[1] 1 3
> a[a>2]
[1] 4 3
> a[a>3]
[1] 4
> a[a==3]
[1] 3
> a[a<=3]
[1] 1 2 3

· ②. 向量的排序

> a <- c(1,2,4,3)
> a
[1] 1 2 4 3
> ?sort
> sort(a)
[1] 1 2 3 4
> sort(a,decreasing = T)
[1] 4 3 2 1
> sort(a,index.return=T,decreasing = T)
$x
[1] 4 3 2 1

$ix
[1] 3 4 2 1

· ③. 向量的唯一化

> a <- c("b","b","c","c","d","d")
> a
[1] "b" "b" "c" "c" "d" "d"
> unique(a)
[1] "b" "c" "d"

· ④. 連續(xù)向量的離散化

> a <- c(1,2,4,3)
> a
[1] 1 2 4 3
> a <- cut(a,breaks = c(0,2,4),labels = c("0-2","2-4"),right = T)
> a
[1] 0-2 0-2 2-4 2-4
Levels: 0-2 2-4

· ⑤. 向量的因子化

> a <- c("good","good","fair","fair","very good","very good")
> a
[1] "good"      "good"      "fair"      "fair"      "very good" "very good"
> class(a)
[1] "character"
> a <- as.factor(a)
> a
[1] good      good      fair      fair      very good very good
Levels: fair good very good
> a <- c("good","good","fair","fair","very good","very good")
> a <- factor(a)
> a
[1] good      good      fair      fair      very good very good
Levels: fair good very good
> a <- c("good","good","fair","fair","very_good","very_good")
> a
[1] "good"      "good"      "fair"      "fair"      "very_good" "very_good"
> class(a)
[1] "character"
> a <- as.factor(a)
> a
[1] good      good      fair      fair      very_good very_good
Levels: fair good very_good
> a <- c("good","good","fair","fair","very_good","very_good")
> a <- factor(a)
> a
[1] good      good      fair      fair      very_good very_good
Levels: fair good very_good
> a <- factor(a,levels = c("fair","very_good","good"))
> a
[1] good      good      fair      fair      very_good very_good
Levels: fair very_good good
> a <- factor(a,levels = c("fair","very_good","good"),ordered = T)
> a
[1] good      good      fair      fair      very_good very_good
Levels: fair < very_good < good

· ⑤. 向量類型的轉(zhuǎn)化:

> a <- c("1","2","3","4")
> a
[1] "1" "2" "3" "4"
> a <- as.numeric(a)
> a
[1] 1 2 3 4

· 因子型向量轉(zhuǎn)換成數(shù)值型向量,需要先轉(zhuǎn)換成字符型向量,再?gòu)淖址拖蛄哭D(zhuǎn)化成數(shù)值型向量

> a <- factor(c(1,3,5,2,4),ordered = T)
> a
[1] 1 3 5 2 4
Levels: 1 < 2 < 3 < 4 < 5
> a <- as.numeric(a)
> a
[1] 1 3 5 2 4
> a <- factor(c(1,3,5,2),ordered = T)
> a
[1] 1 3 5 2
Levels: 1 < 2 < 3 < 5
> a <- as.numeric(a)
> a
[1] 1 3 4 2
> a <- factor(c(1,3,5,2),ordered = T)
> a <- as.character(a)
> a
[1] "1" "3" "5" "2"
> a <- as.numeric(a)
> a
[1] 1 3 5 2

· 2. 數(shù)據(jù)框:一般用行來表示觀測(cè),用列來表示變量。
· 你可以把數(shù)據(jù)框看作由不同數(shù)據(jù)類型的向量組成的集合。
· data.frame函數(shù)可以構(gòu)建數(shù)據(jù)框

> df <- data.frame()  # 空數(shù)據(jù)集
> df
data frame with 0 columns and 0 rows
> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> df
  x y z
1 a 1 2
2 b 2 5
3 c 3 6

· 查看數(shù)據(jù)框各列的數(shù)據(jù)類型

> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> class(df$x)
[1] "character"
> class(df$y)
[1] "integer"
> class(df$z)
[1] "numeric"
> str(df)
'data.frame':   3 obs. of  3 variables:
 $ x: chr  "a" "b" "c"
 $ y: int  1 2 3
 $ z: num  2 5 6

· view函數(shù)、head函數(shù)tail函數(shù)可以查看數(shù)據(jù)框的基本情況

> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> View(df)
> head(df)
  x y z
1 a 1 2
2 b 2 5
3 c 3 6
> head(df,1)
  x y z
1 a 1 2
> tail(df)
  x y z
1 a 1 2
2 b 2 5
3 c 3 6
> tail(df,5)
  x y z
1 a 1 2
2 b 2 5
3 c 3 6

· 可以通過符號(hào)$[]來取數(shù)據(jù)框某一列的內(nèi)容

> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> df$x
[1] "a" "b" "c"
> df[,1]
[1] "a" "b" "c"
> df[,c(1,2)]
  x y
1 a 1
2 b 2
3 c 3
> df[,"x"]
[1] "a" "b" "c"
> df[,c("x","z")]
  x z
1 a 2
2 b 5
3 c 6
> df
  x y z
1 a 1 2
2 b 2 5
3 c 3 6
> df[1,2]
[1] 1
> df[1,"x"]
[1] "a"
> df[1,x]
Error in `[.data.frame`(df, 1, x) : undefined columns selected

· 獲取數(shù)據(jù)框的行數(shù)、列數(shù)、行名列名

> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> nrow(df)
[1] 3
> ncol(df)
[1] 3
> dim(df)
[1] 3 3
> rownames(df)
NULL
> colnames(df)
[1] "x" "y" "z"
> rownames(df) <- c("A","B","C")
> View(df)
> df["A",]
  x   y   z 
"a" "1" "2" 
> df[1,]
  x   y   z 
"a" "1" "2" 
> colnames(df) <- c("X","Y","Z")
> View(df)

· 3. 矩陣:也可以看做多個(gè)向量的合集,但這些向量必須是相同類型的。矩陣的操作和數(shù)據(jù)框差不多,但不能用符號(hào)$來提取列的內(nèi)容

> df <- data.frame(x=c("a","b","c"),y=1:3,z=c(2,5,6))
> class(df)
[1] "data.frame"
> df <- as.matrix(df)
> class(df)
[1] "matrix" "array"
> df[,1]
[1] "a" "b" "c"
> df[1,]
  x   y   z 
"a" "1" "2" 
> df[1,1]
  x 
"a" 
> class(df[1,1])
[1] "character"
> df[,c(1,2)]
     x   y  
[1,] "a" "1"
[2,] "b" "2"
[3,] "c" "3"

· 4. 列表:由分量組成的集合,這些分量可以是向量、數(shù)據(jù)框矩陣。

> dat <-list( c(1,2) , data.frame(number=c(1,2),aplha=c("a","b")),
matrix(c(1,2,3,4),nrow = 2))
> View(dat)
> dat[[1]]
[1] 1 2
> dat[[2]]
  number aplha
1      1     a
2      2     b
> dat[[3]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容