97期学习小组Day6笔记--

作者: 五官端正123 | 来源:发表于2021-01-28 00:00 被阅读0次

查询CRAN和Bioconductor的下载镜像代码

options()$repos
options()$BioC_mirror

下面开始设置


1. 初级模式

图片来自于生信星球

2. 升级模式

使用下面这段代码

# options函数就是设置R运行过程中的一些选项设置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
# 当然可以换成其他地区的镜像

但是这种方法还是有问题,你下次再打开Rstudio会发现,下载Bioconductor还是会回到官方镜像

3. 高级模式
可以直接通过设置R配置文件.Rprofile
在RStudio中输入

file.edit('~/.Rprofile')

在打开的Rprofile中输入如下代码

options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源

然后保存重启RStudio即可。

  • 安装、加载R包

dplyr包为例

  • 安装

install.packages(“dplyr”) #包在CRAN网站
#BiocManager::install(“dplyr”) #包在Biocductor网站

选择哪个代码,可在网上搜索包在哪个网站,有时二者均可。

  • 加载

library(dplyr) #加载dplyr包
#require(dplyr) #加载dplyr包,两句代码一个意思

示例数据使用内置数据集iris的简化版

test <- iris[c(1:2,51:52,101:102),] # 取iris数据集中的1,2,51,52,101,102行做为test数据框
  • 常用基础函数

  • mutate()新增列

mutate(test, new = Sepal.Length * Sepal.Width) 
#在test数据框中新增new列,内容是Sepal.Length 乘 Sepal.Width
  • select()按列筛选

  1. 按列号
    select(test,1) # 筛选test中的第一列
    select(test,c(1,5)) #筛选test中的第1列和第5列
  2. 按列名
    select(test,Sepal.Length) #筛选test中的Sepal.Length列
    select(test, Petal.Length, Petal.Width) #筛选test中的Petal.Length和Petal.Width列
  • filter()筛选行

filter(test, Species == "setosa") #筛选test中的setosa行
filter(test, Species == "setosa"&Sepal.Length > 5 ) #筛选test中的setosa行,并且Sepal.Length值 > 5的行
filter(test, Species %in% c("setosa","versicolor")) #筛选test中的Species列中内容为"setosa","versicolor"的行

  • arrange() 按某1列或某几列对整个表格进行排序

arrange(test, Sepal.Length) #test中Sepal.Length值排序
#默认从小到大排序,即升序
arrange(test, desc(Sepal.Length)) #降序排序
  • summarise()汇总

summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length)) #首先按照Species分组,然后计算每组Sepal.Length的平均值和标准差
  • dplyr两个实用技能

  • 管道操作%>%(快捷键:CMD+shift+M)

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

自己的理解,%>%后面的所有操作都是在前面的基础之上进行的,相当于前面的基础是一个管道

通过查阅资料,管道操作适用于操作步骤少的代码组合,可以避免产生较多的无用变量。

  • count,统计某列的unique值

count(test,Species) #统计test中,Species列的个数

  • dplyr处理关系数据

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
  • 内连inner_join取交集

inner_join(test1, test2, by = "x")
##   x z y
## 1 b A 2
## 2 e B 5
## 3 f C 6
  • 左连left_join

left_join(test1, test2, by = 'x') #以test1为主,谁在前以谁为主
##   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') #以test2为主,谁在前以谁为主
##   x y    z
## 1 a 1 
## 2 b 2    A
## 3 c 3 
## 4 d 4 
## 5 e 5    B
## 6 f 6    C
  • 全连full_join

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 
## 6 c 
## 7 d 
  • semi_join半连接

返回能够与y表匹配的x表所有记录

semi_join(x = test1, y = test2, by = 'x')
##   x z
## 1 b A
## 2 e B
## 3 f C
  • anti_join 反连接

返回无法与y表匹配的x表的所记录anti_join

anti_join(x = test2, y = test1, by = 'x')
##   x y
## 1 a 1
## 2 c 3
## 3 d 4
  • bind_rows(),bind_cols()

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

相关文章

网友评论

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

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