今天首先按照教程进行了镜像设置
file.edit('~/.Rprofile')
添加代码
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
安装
联网后下载r包,安装命令为install.packages(“包”)或者BiocManager::install(“包”)
需要谷歌搜索安装的包存在于CRAN网站还是Biocductor
加载
library(包)或require(包)
举例(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)
代码实操
1.mutate(),新增列
mutate(表名, new = Sepal.Length * Sepal.Width)
2.select(),按列筛选
select(表名,列号/列名/某几列)
特殊:vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
3.filter()筛选行
filter(表名,列名 == "某数值")
4.arrange(),按某1列或某几列对整个表格进行排序
arrange(表名, desc(列名))#用desc从大到小 (默认从小到大)
5.summarise():汇总
举例:
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))
先分组再计算
1:管道操作 %>% (cmd/ctr + shift + M)
2:count统计某列的unique值
表格间关系
options(stringsAsFactors = F)
导入两表格信息
inner_join(test1, test2, by = "x") 取交集
left_join(test1, test2, by = 'x') 所有表1有x值的全要
full_join全连
4.半连接:返回能够与y表匹配的x表所有记录semi_join
5.反连接:返回无法与y表匹配的x表的所记录anti_join
简单合并
两个表格列数相同bind_rows(),而两个数据框有相同的行数bind_cols()