说起来这个,就必须提到Rstudio最重要的两个配置文件:在刚开始运行Rstudio的时候,程序会查看许多配置内容,其中一个就是.Renviron,它是为了设置R的环境变量(这里先不说它);而.Rprofile就是一个代码文件,如果启动时找到这个文件,那么就替我们先运行一遍(这个过程就是在启动Rstudio时完成的) ------微信公众号:生信星球
首先用file.edit('~/.Rprofile')
打开.Rprofile文件;然后在.Rprofile文件内添加下列两行代码
# options函数就是设置R运行过程中的一些选项设置
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
# 当然可以换成其他地区的镜像
最后保存.Rprofile文件=》重启Rstudio,这时你再运行一下options()$repos #查看CRAN包的镜像
和options()$BioC_mirror #查看Bioconductor包的镜像
就发现已经镜像已经配置好了,不需要每次打开再重新配置了。
install.packages(“包”)
:安装CRAN官网的包
BiocManager::install(“包”)
:安装Biocductor的包library(包)
或者require(包)
Rstudio中包只需要安装一次,但每次启动都需要重新加载R包以R自带的iris数据框为例
test <- iris[c(1:2,51:52,101:102),]
mutate(test, new = Sepal.Length * Sepal.Width) #test数据框新增一列命名为new
#筛选一列
select(test,1)
#筛选多列
select(test,c(1,5))
(2)按列名筛选
#筛选一列
select(test,Sepal.Length)
#筛选多列
#方法1
select(test, Petal.Length, Petal.Width)
#方法2
vars <- c("Petal.Length", "Petal.Width")
select(test, one_of(vars))
filter(test, Species == "setosa")
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
filter(test, 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"))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 7.0 3.2 4.7 1.4 versicolor
## 4 6.4 3.2 4.5 1.5 versicolor
arrange(test, Sepal.Length)#按照Sepal.Length这一列排序,默认从小到大排序
arrange(test, desc(Sepal.Length))#用desc从大到小
summarise(test, mean(Sepal.Length), sd(Sepal.Length))# 计算Sepal.Length的平均值和标准差
# 先按照Species分组,计算每组Sepal.Length的平均值和标准差
group_by(test, Species) #test按照Species分组
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
管道操作是一种强大的工具,能够通过管道将数据从一个函数传给另外一个函数,从而用若干函数构成的管道依次变换你的数据。管道运算符号为%>%(Windows快捷键为Shift+CTRL+M),其意思是将左边的运算结果,以输入的方式传递给右边的函数,若干个函数通过管道连接起来,叫做管道(pipeline)。来自于Stefan大神开发的magrittr包,因为加载tidyverse包的时候,会自动加载该包,所以无需单独加载magrittr包。 -----来自https://zhuanlan.zhihu.com/p/443548010
x %>% f() %>% g() #等同于g(f(x))
test %>%
group_by(Species) %>%
summarise(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
count(test,Species)
## # A tibble: 3 x 2
## Species n
##
## 1 setosa 2
## 2 versicolor 2
## 3 virginica 2
即将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))
inner_join(test1, test2, by = "x")
left_join(test1, test2, by = 'x') # 全保留test1,合并test2能匹配上的数据
left_join(test2, test1, by = 'x') # 全保留test2,合并test1能匹配上的数据
# NA与<NA>的区别:前者为数字型NA,后者为字符型NA
full_join( test1, test2, by = 'x') #不在乎test1与test2是否匹配
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_rows(test1, test2)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。