生信星球培训第六十九期

学习小组Day6笔记-田熙

2020-07-09  本文已影响0人  田熙

学习一个R包

安装R包

如果要安装的R包在CRAN网站,那么输入install.packages(“包”), 如果R包在Bioconductor,那么输入BiocManager::install(“包”)

dplyr的运用

image.png

按照示例数据加载测试数据之后,本想看一看test数据的样子,发现除了点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)#默认从小到大排序
  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的平第一个和最大值
  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、管道操作:相当于走了捷径

> 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统计unique值

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

今天暂时到这

上一篇 下一篇

猜你喜欢

热点阅读