生信星球培训第四十二期

学习小组Day6笔记--lxl

2020-03-11  本文已影响0人  lxl_26

思维导图

R包安装.PNG

配置镜像

参考生信星球https://mp.weixin.qq.com/s/XvKb5FjAGM6gYsxTw3tcWw

初级模式

初始配置 生信星球.PNG

缺点:这个是CRAN的镜像,如果要下载Bioconductor的包,这个镜像是没有办法用的。

升级模式

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

缺点:下次再打开Rstudio会发现,下载Bioconductor还是会回到官方镜像。可以查询options()$BioC_mirror试试,如果你的依然是自己设置的国内镜像,就不用管了;如果发现需要再重新运行一遍代码进行设置。

一劳永逸的高级模式

file.edit('~/.Rprofile')
然后在其中添加好上面的options代码,保存,重启Rstudio

安装包

`install.packages(“包”)`
`BiocManager::install(“包”)`

加载包

`library(包)`
`require(包)`

R包的讲解(dplyr为例)

dplyr包,d=data.frame,是专门用于数据框的

dplyr基础函数

示例数据

test <- iris[c(1:2,51:52,101:102),]
mutate(test, new = Sepal.Length * Sepal.Width)
select(test,c(1,5))
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))
summarise(test, mean(Sepal.Length), sd(Sepal.Length))
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))

实用技能

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

处理数据关系

示例数据

options(stringsAsFactors = F) #注意:不要引入factor
test1 <- data.frame(x = c('b','e','f','x'), 
                    z = c("A","B","C",'D'),
                    stringsAsFactors = F)
test2 <- data.frame(x = c('a','b','c','d','e','f'), 
                    y = c(1,2,3,4,5,6),
                    stringsAsFactors = F)
inner_join(test1, test2, by = "x")
left_join(test1, test2, by = 'x')
left_join(test2, test1, by = 'x')
full_join( test1, test2, by = 'x')
semi_join(x = test1, y = test2, by = 'x')
anti_join(x = test2, y = test1, by = 'x')
test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
test2 <- data.frame(x = c(5,6), y = c(50,60))
test3 <- data.frame(z = c(100,200,300,400))
bind_rows(test1, test2)
bind_cols(test1, test3)
上一篇 下一篇

猜你喜欢

热点阅读