今天是学习小组学习的第6天,主要是学习了解R包
1. 安装和加载R包
R包是多个函数的集合
(1)在Rstudio程序设置中设置,可以用options()$repos来检验,但有时候还是不能成功,也不能下载Bioconductor的包
(2)代码设置
# options函数就是设置R运行过程中的一些选项设置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
# 可以换成其他地区的镜像
但这种方法每次打开Rstudio都要运行一遍镜像配置,可以查询options()$BioC_mirror 检验
(3) 用到R的配置文件 .Rprofile
确保联网后,用命令nstall.packages(“包”)或者BiocManager::install(“包”)安装需要的包
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)
# 新增一列new
# (1)按列号筛选
select(test,1)
select(test,c(1,5))
selec(test,Sepal.Length)
# (2)按列名筛选
select(test, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
filter(test, Species == "setosa")
filter(test, Species == "setosa"&Sepal.Length > 5 )
filter(test, Species %in% c("setosa","versicolor"))
arrange(test, Sepal.Length)#默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小
对数据进行汇总,结合group_by实用性强
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差
## mean(Sepal.Length) sd(Sepal.Length)
## 1 5.916667 0.8084965
# 先按照Species分组,计算每组Sepal.Length的平均值和标准差
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))
## # A tibble: 3 x 3
## Species `mean(Sepal.Length)` `sd(Sepal.Length)`
##
## 1 setosa 5 0.141
## 2 versicolor 6.7 0.424
## 3 virginica 6.05 0.354
(加载任意一个tidyverse包即可用管道符号)
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
#管道上一个输出赋给了下一个输入
count(test,Species)
## # A tibble: 3 x 2
## Species n
##
## 1 setosa 2
## 2 versicolor 2
## 3 virginica 2
连接两表
test1 <- data.frame(x = c('b','e','f','x'),
z = c("A","B","C",'D'))
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))
test2
## x y
## 1 a 1
## 2 b 2
## 3 c 3
## 4 d 4
## 5 e 5
## 6 f 6
inner_join(test1, test2, by = "x") #不加by会自动获取相同的列名
## x z y
## 1 b A 2
## 2 e B 5
## 3 f C 6
left_join(test1, test2, by = 'x') #以左边为准
## 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') #右连接则是right_join
## 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
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 <NA> 1
## 6 c <NA> 3
## 7 d <NA> 4
semi_join(x = test1, y = test2, by = 'x')
## x z
## 1 b A
## 2 e B
## 3 f C
anti_join(x = test2, y = test1, by = 'x')
## x y
## 1 a 1
## 2 c 3
## 3 d 4
相当于base包里的cbind()函数和rbind()
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
示例代码来自微信公众号生信星球
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。