學(xué)習(xí)小組Day6-Xy

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

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

生信必備Biocductor上生信分析R包
以dplyr為例

  1. 配置Rstudio鏡像
#代碼源生信星球
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #對(duì)應(yīng)清華源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #對(duì)應(yīng)中科大源
  1. 安裝
    install.packages(' ')
    BiocManager::install(" ")
  2. 加載
    library()
    require()

dplyr五個(gè)基礎(chǔ)函數(shù)

> test <-  iris[c(1:2,51:52,101:102),]
> test
    Sepal.Length Sepal.Width Petal.Length Petal.Width
1            5.1         3.5          1.4         0.2
2            4.9         3.0          1.4         0.2
51           7.0         3.2          4.7         1.4
52           6.4         3.2          4.5         1.5
101          6.3         3.3          6.0         2.5
102          5.8         2.7          5.1         1.9
       Species
1       setosa
2       setosa
51  versicolor
52  versicolor
101  virginica
102  virginica
> library(dplyr)
  1. mutate() 新增列
> mutate(test,new=Sepal.Length* Sepal.Width)
    Sepal.Length Sepal.Width Petal.Length Petal.Width
1            5.1         3.5          1.4         0.2
2            4.9         3.0          1.4         0.2
51           7.0         3.2          4.7         1.4
52           6.4         3.2          4.5         1.5
101          6.3         3.3          6.0         2.5
102          5.8         2.7          5.1         1.9
       Species   new
1       setosa 17.85
2       setosa 14.70
51  versicolor 22.40
52  versicolor 20.48
101  virginica 20.79
102  virginica 15.66
  1. select() 按列篩選
    按列號(hào)篩選
    select(test,1)
    select(test,c(1,5))
    select(test,Sepal.Length)
    按列名篩選
    select(test, Petal.Length, Petal.Width)

    vars <- c("Petal.Length", "Petal.Width")
    select(test, one_of(vars))

> select(test,Sepal.Length)
    Sepal.Length
1            5.1
2            4.9
51           7.0
52           6.4
101          6.3
102          5.8
  1. filter()篩選行
filter(test, Species == "setosa")
#篩選Species == setosa的行
filter(test, Species == "setosa"&Sepal.Length > 5 )
#篩選Species == setosa并且Sepal.Length>5的行
filter(test, Species %in% c("setosa","versicolor"))
#篩選Species 屬于 setosa或versicolor的行
  1. arrange(),按某1列或某幾列隊(duì)整個(gè)表格進(jìn)行排序
arrange(test, Sepal.Length)
#默認(rèn)從小到大排序
arrange(test, desc(Sepal.Length))
#用desc從大到小

5.summarise()匯總

summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 計(jì)算Sepal.Length的平均值和標(biāo)準(zhǔn)差

# 先按照Species分組,計(jì)算每組Sepal.Length的平均值和標(biāo)準(zhǔn)差
group_by(test, Species)

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

dplyr兩個(gè)實(shí)用技能

  1. 管道操作 %>% (cmd/ctr + shift + M)
test %>% 
  group_by(Species) %>% 
  summarise(mean(Sepal.Length), sd(Sepal.Length))
###等同于summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
  1. count統(tǒng)計(jì)某列的unique值
    count(test,Species)

dplyr 處理關(guān)系數(shù)據(jù)

> options(stringsAsFactors = F)
> test1 <- data.frame(x = c('b','e','f','x'), 
+                     z = c("A","B","C",'D'),
+                     stringsAsFactors = F)
> test1
  x z
1 b A
2 e B
3 f C
4 x D
> test2 <- data.frame(x = c('a','b','c','d','e','f'), 
+                     y = c(1,2,3,4,5,6),
+                     stringsAsFactors = F)
> test2 
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

The mutating joins add columns from y to x, matching rows based on the keys:
inner_join(): includes all rows in x and y.
left_join(): includes all rows in x.
right_join(): includes all rows in y.
full_join(): includes all rows in x or y.
If a row in x matches multiple rows in y, all the rows in y will be returned once for each matching row in x.

> inner_join(test1,test2,by='x')
  x z y
1 b A 2
2 e B 5
3 f C 6
> 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
> 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
> semi_join(test1,test2,by='x')
  x z
1 b A
2 e B
3 f C
> anti_join(test1,test2,by='x')
  x z
1 x D
> anti_join(test2,test1,by='x')
  x y
1 a 1
2 c 3
3 d 4

簡(jiǎn)單合并
cbind() rbind()
bind_rows()函數(shù)需要兩個(gè)表格列數(shù)相同,而bind_cols()函數(shù)則需要兩個(gè)數(shù)據(jù)框有相同的行數(shù)

最后編輯于
?著作權(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)容