day6-白雪
2023-02-25 18:19:49 时间
引用于微信公众号生信星球须知
R包是多个函数的集合,具有详细的说明和示例。#含有多个函数使用的代码以及方法
R包的安装和加载
- 镜像设置
# options函数就是设置R运行过程中的一些选项设置
> options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
> options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
> options()$BioC_mirror #查看镜像
[1] "https://mirrors.ustc.edu.cn/bioc/"
在这个过程中你可能会发现问题,例如下次在进到rstudio的话,查看镜像,又不在了,怎么办呢
说起来这个,就必须提到Rstudio最重要的两个配置文件:在刚开始运行Rstudio的时候,程序会查看许多配置内容,其中一个就是.Renviron,它是为了设置R的环境变量(这里先不说它);而.Rprofile就是一个代码文件,如果启动时找到这个文件,那么就替我们先运行一遍(这个过程就是在启动Rstudio时完成的)
就是在运行Rstudio的时候,先读一下.Rprofile中的代码
用
file.edit('~/.Rprofile') #编辑.Rprofile
之后在脚本编辑区输入设置镜像的代码
保存,重启Rstudio即可
- 安装 (必须要联网)
R包的安装命令
R包安装命令是install.packages(“包”)或者BiocManager::install(“包”)。取决于你要安装的包存在于CRAN网站还是Biocductor,存在于哪里?可以谷歌搜到。,首先得知道你要安装什么包,安装包完成后,才可以使用包里面的函数 已安装dplyr为例
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) #dplyr下载的是一个安装包,解压在输,要不报错
示例数据直接使用内置数据集iris的简化版:
test <- iris[c(1:2,51:52,101:102),]
dplyr 的五个基础函数
- mutate(),新增列
> mutate(test, new = Sepal.Length * Sepal.Width) #新增一列,列名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
- select(),按列筛选
- 按列号筛选
> 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这一列
Sepal.Length
1 5.1
2 4.9
51 7.0
52 6.4
101 6.3
102 5.8
- 按列名筛选
> select(test, Petal.Length, Petal.Width) #筛选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") #赋值vars为c("Petal.Length", "Petal.Width")
> select(test, one_of(vars)) #筛选test中,跟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
- filter()筛选行
> filter(test, Species == "setosa") #筛选Species是setosa的行
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
Species
1 setosa
2 setosa
> filter(test, Species == "setosa"&Sepal.Length > 5 ) #筛选Species是setosa的行,并且Sepal.length大于5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
> filter(test, Species %in% c("setosa","versicolor")) #筛选Species 在c("setosa","versicolor") 中的行
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
3 7.0 3.2 4.7 1.4
4 6.4 3.2 4.5 1.5
Species
1 setosa
2 setosa
3 versicolor
4 versicolor
- arrange(),按某1列或某几列对整个表格进行排序
> arrange(test, Sepal.Length) ##默认Sepal.Lengt从小到大排序
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 4.9 3.0 1.4 0.2
2 5.1 3.5 1.4 0.2
3 5.8 2.7 5.1 1.9
4 6.3 3.3 6.0 2.5
5 6.4 3.2 4.5 1.5
6 7.0 3.2 4.7 1.4
Species
1 setosa
2 setosa
3 virginica
4 virginica
5 versicolor
6 versicolor
> arrange(test, desc(Sepal.Length)) #用desc对Sepal.Leng从大到小
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 7.0 3.2 4.7 1.4
2 6.4 3.2 4.5 1.5
3 6.3 3.3 6.0 2.5
4 5.8 2.7 5.1 1.9
5 5.1 3.5 1.4 0.2
6 4.9 3.0 1.4 0.2
Species
1 versicolor
2 versicolor
3 virginica
4 virginica
5 setosa
6 setosa
- summarise():汇总
> summarise(test, mean(Sepal.Length), sd(Sepal.Length)) #mean平均值,sd:标准差
mean(Sepal.Length) sd(Sepal.Length)
1 5.916667 0.8084965
> group_by(test, Species) #把test的内容按照Species进行分组
# A tibble: 6 × 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 versicol…
4 6.4 3.2 4.5 1.5 versicol…
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)) #计算每组的 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
dplyr
- 1:管道操作 %>% (cmd/ctr + shift + M) #我们可以将其理解为车间里的流水线,经过前一步加工的产品才能进入后一步进一步加工,其作用是将前一步的结果直接传参给下一步的函数,从而省略了中间的赋值步骤,可以大量减少内存中的对象,节省内存。引用自微信公众号生信星球
- count统计某列的unique值 计算数据对象(vector、dataframe)的unique独特值: unique函数 从vector向量、dataframe 中 删除重复项、删除dataframe重复行
> count(test,Species)
Species n
1 setosa 2
2 versicolor 2
3 virginica 2
dplyr处理关系数据
即将2个表进行连接
> test1 <- data.frame(x = c('b','e','f','x'),
+ z = c("A","B","C",'D'))
> test2 <- data.frame(x = c('a','b','c','d','e','f'),
+ y = c(1,2,3,4,5,6))
> 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
- 1.內连inner_join,取交集
> inner_join(test1, test2, by = "x") #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') #test1不变,test2在x处相等的内容显示出来,没有的为NA
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') #将test1和test2根据X合并成一个,没有的内容直接显示NA
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.半连接:返回能够与y表匹配的x表所有记录semi_join
> semi_join(x = test1, y = test2, by = 'x') #显示出x的位置,1表与2表相匹配的元素
x z
1 b A
2 e B
3 f C
- 5.反连接:返回无法与y表匹配的x表的所记录anti_join
> anti_join(x = test2, y = test1, by = 'x') #显示2表中x与1表不同的数据
x y
1 a 1
2 c 3
3 d 4
- 简单合并
bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数
> 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
复习
相关文章
- 8天学通MongoDB——第八天 驱动实践
- 8天学通MongoDB——第七天 运维技术
- 8天学通MongoDB——第六天 分片技术
- 8天学通MongoDB——第五天 主从复制
- 8天学通MongoDB——第四天 索引操作
- 8天学通MongoDB——第三天 细说高级操作
- 8天学通MongoDB——第二天 细说增删查改
- 8天学通MongoDB——第一天 基础入门
- PHP实现常见排序
- PHP天坑总结
- mac必备软件Go2shell
- 在github的某次commit中close或者fix某个issue
- 将你的PHP程序升级到PHP7.0
- go下载
- Centos搭建GIT服务器
- golang使用multiconfig后导致glog无法接受命令行参数
- 关闭OSX的rootless和修改MAMP的php.ini配置
- nginx+php 上传大文件
- 让 PHPSTORM 更智能的支持 Laravel
- docker使用笔记VI -- PHP