學(xué)習(xí)小組Day6筆記-田熙

學(xué)習(xí)一個(gè)R包

安裝R包

如果要安裝的R包在CRAN網(wǎng)站,那么輸入install.packages(“包”), 如果R包在Bioconductor,那么輸入BiocManager::install(“包”)。

dplyr的運(yùn)用

image.png

按照示例數(shù)據(jù)加載測試數(shù)據(jù)之后,本想看一看test數(shù)據(jù)的樣子,發(fā)現(xiàn)除了點(diǎn)bug,view的v要大寫才可以。

1、mutate():新增列

mutate(test, new = Petal.Length*Petal.Width)

image.png

2、select():按列篩選

> select(test,2)
    Sepal.Width
1           3.5
2           3.0
51          3.2
52          3.2
101         3.3
102         2.7
> select(test,c(1,4))
    Sepal.Length Petal.Width
1            5.1         0.2
2            4.9         0.2
51           7.0         1.4
52           6.4         1.5
101          6.3         2.5
102          5.8         1.9
> select(test, Sepal.Length, Petal.Width)
    Sepal.Length Petal.Width
1            5.1         0.2
2            4.9         0.2
51           7.0         1.4
52           6.4         1.5
101          6.3         2.5
102          5.8         1.9
> select(test, one_of(c("Petal.Length", "Petal.Width")))
    Petal.Length Petal.Width
1            1.4         0.2
2            1.4         0.2
51           4.7         1.4
52           4.5         1.5
101          6.0         2.5
102          5.1         1.9

3、filter():按行篩選

> filter(test, Sepal.Width ==3.2)
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1          7.0         3.2          4.7         1.4 versicolor
2          6.4         3.2          4.5         1.5 versicolor
> filter(test, Sepal.Width ==3.2 & Petal.Length>4.5)
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            7         3.2          4.7         1.4 versicolor
> filter(test, Species %in% c("setosa","virginica"))
  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.3         3.3          6.0         2.5 virginica
4          5.8         2.7          5.1         1.9 virginica

4、arrange():自定義排序

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

5、summarise():匯總

> ?summarise()
> summarise(test, first(Sepal.Length), max(Sepal.Length))# Sepal.Length的平第一個(gè)和最大值
  first(Sepal.Length) max(Sepal.Length)
1                 5.1                 7
> group_by(test, Species)
# A tibble: 6 x 5
# Groups:   Species [3]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   
         <dbl>       <dbl>        <dbl>       <dbl> <fct>     
1          5.1         3.5          1.4         0.2 setosa    
2          4.9         3            1.4         0.2 setosa    
3          7           3.2          4.7         1.4 versicolor
4          6.4         3.2          4.5         1.5 versicolor
5          6.3         3.3          6           2.5 virginica 
6          5.8         2.7          5.1         1.9 virginica 
> summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
  Species    `mean(Sepal.Length)` `sd(Sepal.Length)`
  <fct>                     <dbl>              <dbl>
1 setosa                     5                 0.141
2 versicolor                 6.7               0.424
3 virginica                  6.05              0.354

6、管道操作:相當(dāng)于走了捷徑

> test %>% 
+     group_by(Species) %>% 
+     summarise(mean(Sepal.Length), sd(Sepal.Length))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
  Species    `mean(Sepal.Length)` `sd(Sepal.Length)`
  <fct>                     <dbl>              <dbl>
1 setosa                     5                 0.141
2 versicolor                 6.7               0.424
3 virginica                  6.05              0.354

7、count統(tǒng)計(jì)unique值

> count(test,Species)
     Species n
1     setosa 2
2 versicolor 2
3  virginica 2

今天暫時(shí)到這

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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