Day6 學(xué)習(xí)R包

鏡像設(shè)置

1.菜單法 可以配置CRAN鏡像,一般選清華

*但是bioconductor上R包沒(méi)法用這個(gè)CRAN鏡像下載,而且無(wú)法保證一定從此CRAN下載R包。

options()$repos  
options()$BioC_mirror
#可以通過(guò)此命令查找目前Rstudio通過(guò)什么鏡像下載R包

2.代碼法

*同時(shí)設(shè)置CRAN鏡像及bioconductor鏡像

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))  #清華源CRAN
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #中科大源bioconductor

*每次啟動(dòng)都需要先運(yùn)行。

3.高級(jí)模式 一勞永逸

R的兩個(gè)最重要的配置文件
.Renviron #設(shè)置R的環(huán)境變量用,此處按下不表
.Rprofile #R的代碼文件,啟動(dòng)Rstudio時(shí)自動(dòng)運(yùn)行
在Linux中可以使用file.edit命令編輯

file.edit(~/.Rprofile)  #注意此處是Linux命令

然后在出現(xiàn)的對(duì)話框中添加2的上述兩行option代碼,即可配置完成

安裝R包

#首先google一下想安的包在CRAN還是bioconductor, 再分別選擇使用以下命令
install.packages(“包”)
BiocManager::install(“包”)

加載

library(包)
require(包)

學(xué)習(xí)dplyr包

加載“dplyr"

此前已通過(guò)“tydiverse”安裝“dplyr”,此處直接加載

library("dplyr")

學(xué)一下"dplyr"

help(“dplyr")
browseVignettes(package = "dplyr")
#找到dplyr的網(wǎng)絡(luò)介紹鏈接:[Introduction to dplyr](http://127.0.0.1:19586/library/dplyr/doc/dplyr.html),然后chrome有道網(wǎng)頁(yè)翻譯2.0打開(kāi)右側(cè)窗口顯示,左邊敲Rscript,不能太方便。

單表數(shù)據(jù)操作:都是“動(dòng)詞(表格,命令)”格式

  • filter() #按行篩選
  • arrange() #按列排序
  • select() #按列篩選
  • mutate() transmute() #新增列,該列是現(xiàn)有列的函數(shù)
  • summarise() #匯總
  • sample_n() sample_frac() #隨機(jī)抽樣
  • slice() #按等級(jí)秩序選擇各行,少用,可用filter(), 或row_number()代替

數(shù)據(jù):使用生信星球教學(xué)數(shù)據(jù)集

> test <- iris[c(1:2,51:52,101:102),] #把內(nèi)置數(shù)據(jù)框iris的相應(yīng)行提取出來(lái)賦值給test數(shù)據(jù)框
> test
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
51           7.0         3.2          4.7         1.4 versicolor
52           6.4         3.2          4.5         1.5 versicolor
101          6.3         3.3          6.0         2.5  virginica
102          5.8         2.7          5.1         1.9  virginica

* filter() #按行篩選

> filter(test, Species == "setosa")
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
 #等同于,
test[test$Species == "setosa",] 
> filter(test, Species == "setosa"&Sepal.Length > 5 )
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
#等同于,
test[test$Species == "setosa" & test$Sepal.Length > 5,]
> filter(test, Species %in% c("setosa","versicolor"))
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.1         3.5          1.4         0.2     setosa
2          4.9         3.0          1.4         0.2     setosa
3          7.0         3.2          4.7         1.4 versicolor
4          6.4         3.2          4.5         1.5 versicolor
#等同于,
test[test$Species %in% c("setosa","versicolor"),]

* arrange() #按列排序

> arrange(test, Sepal.Length)  #默認(rèn)從小到大排序
> arrange(test, desc(Sepal.Length))  #用desc從大到小
> arrange(test, Petal.Length, desc(Sepal.Length)) #先按變量1從小到大,再按變量2從大到小
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          5.1         3.5          1.4         0.2     setosa
2          4.9         3.0          1.4         0.2     setosa
3          6.4         3.2          4.5         1.5 versicolor
4          7.0         3.2          4.7         1.4 versicolor
5          5.8         2.7          5.1         1.9  virginica
6          6.3         3.3          6.0         2.5  virginica

*select() #按列篩選

#具體用法,
select(test,1:3) #也可以select(test,Sepal.Length:Petal.Length)
select(test,c(1,5))
select(test,Sepal.Length)
select(test,- c(1,5))  # -號(hào)可做反選
#select() 可以選擇并同時(shí)重命名變量,但不如rename()好用
#可搭配starts_with(),ends_with(),matches()和contains()使用風(fēng)味更佳 具體可[?select](R Documentation
:Select/rename variables by name)看Examples

* mutate() transmute() #新增列,該列是 現(xiàn)有列的函數(shù)

#注意允許引用剛剛創(chuàng)建的列來(lái)新建列,例如,
 mutate(test, new = Sepal.Length * Sepal.Width, new100 = new * 100)
      Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   new new100
1          5.1         3.5          1.4         0.2     setosa 17.85   1785
2          4.9         3.0          1.4         0.2     setosa 14.70   1470
3          7.0         3.2          4.7         1.4 versicolor 22.40   2240
4          6.4         3.2          4.5         1.5 versicolor 20.48   2048
5          6.3         3.3          6.0         2.5  virginica 20.79   2079
6          5.8         2.7          5.1         1.9  virginica 15.66   1566
# 假如只想保存新變量,用transmute()
> transmute(test, new = Sepal.Length * Sepal.Width, new100 = new * 100)
    new new100
1 17.85   1785
2 14.70   1470
3 22.40   2240
4 20.48   2048
5 20.79   2079
6 15.66   1566

* summarise() #匯總, 可結(jié)合group_by()使用

# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
# 先按照Species分組,計(jì)算每組Sepal.Length的平均值和標(biāo)準(zhǔn)差
group_by(test, Species)
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
#summarise描述統(tǒng)計(jì)[?summarise](Reduce multiple values down to a single value)

* sample_n() sample_frac() #隨機(jī)抽樣

#隨機(jī)抽取n個(gè)樣本或幾個(gè)比例frac樣本
> sample_n(test,2)
  Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
1          6.3         3.3          6.0         2.5 virginica
2          4.9         3.0          1.4         0.2    setosa
> sample_frac(test,1/6)
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            7         3.2          4.7         1.4 versicolor

dplyr支持

1.管道操作 %>% (cmd/ctr + shift + M)

test %>% 
  group_by(Species) %>% 
  summarise(mean(Sepal.Length), sd(Sepal.Length))

2:count統(tǒng)計(jì)某列的unique值

count(test,Species)

dplyr雙表操作:處理關(guān)系數(shù)據(jù)

#新建2表,注意不要引入factor,
options(stringsAsFactors = F)
> test1 <- data.frame(x = c('b','e','f','x'), z = c("A","B","C",'D'),stringsAsFactors = F)
> test2 <- data.frame(x = c('a','b','c','d','e','f'), y = c(1,2,3,4,5,6),stringsAsFactors = F)
> test1
  x z
1 b A
2 e B
3 f C
4 x D
> test2
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

開(kāi)始造

1.內(nèi)連inner_join,取交集

> inner_join(test1, test2, by = "x")
  x z y
1 b A 2
2 e B 5
3 f C 6

2.左連left_join

> left_join(test1, test2, by = 'x')
  x z  y
1 b A  2
2 e B  5
3 f C  6
4 x D NA
> left_join(test2, test1, by = 'x')
  x y    z
1 a 1 <NA>
2 b 2    A
3 c 3 <NA>
4 d 4 <NA>
5 e 5    B
6 f 6    C

3.全連full_join

> full_join( test1, test2, by = 'x')
  x    z  y
1 b    A  2
2 e    B  5
3 f    C  6
4 x    D NA
5 a <NA>  1
6 c <NA>  3
7 d <NA>  4

4.半連接semi_join:返回能夠與y表匹配的x表所有記錄

> semi_join(x = test1, y = test2, by = 'x')
  x z
1 b A
2 e B
3 f C

5.反連接anti_join:返回?zé)o法與y表匹配的x表的所記錄

> anti_join(x = test2, y = test1, by = 'x')
  x y
1 a 1
2 c 3
3 d 4

6.簡(jiǎn)單合并:bind_rows()按行合并, bind_cols()按列合并

#相當(dāng)于base包里的cbind()函數(shù)和rbind()函數(shù);注意,bind_rows()函數(shù)需要兩個(gè)表格列數(shù)相同,而bind_cols()函數(shù)則需要兩個(gè)數(shù)據(jù)框有相同的行數(shù)
> test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
> test1
  x  y
1 1 10
2 2 20
3 3 30
4 4 40
> test2 <- data.frame(x = c(5,6), y = c(50,60))
> test2
  x  y
1 5 50
2 6 60
> test3 <- data.frame(z = c(100,200,300,400))
> test3
    z
1 100
2 200
3 300
4 400
> bind_rows(test1, test2)
  x  y
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
6 6 60
> bind_cols(test1, test3)
  x  y   z
1 1 10 100
2 2 20 200
3 3 30 300
4 4 40 400
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 今天學(xué)習(xí)了R包dplyr,總結(jié)一下就是需要學(xué)會(huì) : 設(shè)置鏡像 鏡像的高級(jí)設(shè)置方法(不用每次打開(kāi)Rstudio都得運(yùn)...
    果果爸_2019閱讀 399評(píng)論 0 0
  • 題外話:知乎閱讀三百萬(wàn)的生信學(xué)習(xí)指南嗯哼,無(wú)意中刷到這篇博文,覺(jué)得寫(xiě)的還是蠻接地氣的,就順手關(guān)注了博主的公眾號(hào)生信...
    蒙蒙幫主閱讀 208評(píng)論 0 1
  • 寫(xiě)在前面 R包是多個(gè)函數(shù)的集合,具有詳細(xì)的說(shuō)明和示例。學(xué)生信,R語(yǔ)言必學(xué)的原因是豐富的圖表和Biocductor上...
    小潔忘了怎么分身閱讀 16,987評(píng)論 1 22
  • 學(xué)習(xí)R包 安裝加載R包三部曲 options("repos" = c(CRAN="https://mirrors....
    justsmile_84eb閱讀 357評(píng)論 0 0
  • 1.包和數(shù)據(jù)的安裝 安裝dplyr包 安裝hflights包,該軟件包中的飛機(jī)航班數(shù)據(jù)將用于本文中dplyr包各個(gè)...
    apricoter閱讀 9,487評(píng)論 0 17