美文网首页生信星球培训第134期
学习小组Day6笔记-Young

学习小组Day6笔记-Young

作者: 我有柳叶刀 | 来源:发表于2022-04-23 09:50 被阅读0次

学习R包

一、安装和加载R包

  1. 配置Rstudio下载镜像
    通过options()$repos来查看镜像
# options函数是设置R运行过程中的一些选项设置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
# 可以换成其他地区的镜像
#缺点每次打开都需要重新设置

R的配置文件.Rprofile
使用file.edit()来编辑文件

 file.edit('~/.Rprofile')
截屏2022-04-24 上午9.27.01.png

重启Rstudio,运行

options()$repos
options()$Bioc_mirror #查看配置

2.安装
命令,根据安装包的位置使用下面两个命令
install.packages("包")或者BiocManager::install("包")
3.加载

library(包)
require(包)

二、安装加载的三个命令

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) 
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") 
install.packages("dplyr")
library(dplyr)

测试

test <- iris[c(1:2,51:52,101:102),]
mutate(test, new = Sepal.Length * Sepal.Width)
#    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
#      new
#1   17.85
#2   14.70
#51  22.40
#52  20.48
#101 20.79
#102 15.66

Select(),按列筛选

(1)按列号筛选

> select(test,1)
#    Sepal.Length
#1            5.1
#2            4.9
#51           7.0
#52           6.4
#101          6.3
#102          5.8
> select(test,c(1,5))
 #   Sepal.Length    Species
#1            5.1     setosa
#2            4.9     setosa
#51           7.0 versicolor
#52           6.4 versicolor
#101          6.3  virginica
#102          5.8  virginica
> select(test,Sepal.Length)
#    Sepal.Length
#1            5.1
#2            4.9
#51           7.0
#52           6.4
#101          6.3
#102          5.8

(2)按列名筛选

 select(test, 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
 vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
#    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

dplyr两个使用技能

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

 test %>% 
+     group_by(Species) %>% 
+     summarise(mean(Sepal.Length), sd(Sepal.Length))
# A tibble: 3 × 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

2:count统计某列的unique值

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

dylpr处理关系数据
两个表进行连接
1.简单合并

 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

相关文章

网友评论

    本文标题:学习小组Day6笔记-Young

    本文链接:https://www.haomeiwen.com/subject/wmxtyrtx.html