前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生信入门 第六天

生信入门 第六天

原创
作者头像
用户11153857
发布2024-06-12 21:36:24
1210
发布2024-06-12 21:36:24
举报
文章被收录于专栏:生信星球学习小组170

Today's content is about R packages.

An R package is a set of R functions. Using dplyr as an example to learn R packages.

1. installation and loading

1.1 choose a mirro to download R packages

代码语言:R
复制
options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")

If you are confidient on your internet speed, keep the mirror blank.

1.2 install

use libray() or require(), both OK

代码语言:R
复制
install.packages("dplyr)
library(dplyr)

using simplified iris as demo data

代码语言:R
复制
test <- iris[c(1:2,51:52,101:102),]
View(test)

2. functions in dplyr

(1) mutate() # adds new variables that are functions of existing variables

代码语言:R
复制
mutate(test, new = Sepal.Length * Sepal.Width) # add a new colunm named "new" ,add to the right of the table test, then print the whole table.

(2) select() picks variables based on their names

代码语言:R
复制
select(test, 1)  #pick the 1st colunm of table test, print on the screen.
select(test, c(1,5)) #pick the 1st and 5th column of test, print them on the screen.
select(test, Petal.Length, Petal.Width) # pick by column names.

vars <- c("Petal.Length", "Petal.Width") # define a vector named c, including those two colunm names
select(test, one_of(vars)) # one_of

(3) filter() picks cases based on their values.

代码语言:R
复制
filter(test, Species == "setosa") # in Species column, pick the rows which Species column equals "setosa",  == 是判断第一个向量的每个元素是否等于第二个向量的相对应元素,返回逻辑值
filter(test, Species == "setosa"&Sepal.Length > 5) # 选Species是setosa同时Sepal.Length 大于5的行, & 是 and
filter(test, Species %in% c("setosa","versicolor")) # 选Species 是 setosa和versicolor的行, %in% 是用于判断前一个向量的元素是否在后一个向量中,返回逻辑值。**这句用来查询并抽提数据会非常有用!**

(4) arrange(),按某1列或某几列对整个表格进行排序 changes the ordering of the rows

代码语言:R
复制
arrange(test, Sepal.Length) #按照Sepal.Length的值,重排列行,默认从小到大排序
arrange(test, desc(Sepal.Length)) #用desc从大到小

(5) summarise():汇总 reduces multiple values down to a single summary

代码语言:R
复制
summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # mean()计算Sepal.Length的平均值, sd()计算标准差
# 一个组合应用实例: 先按照Species分组,再计算每组Sepal.Length的平均值和标准差
group_by(test, Species) 
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))

3. two useful skills of dplyr

(1) %>% from the applications in above examples, it is powerful to link diferent steps

代码语言:R
复制
test %>% 
  group_by(Species) %>% 
  summarise(mean(Sepal.Length), sd(Sepal.Length))

(2) count() 统计(计数)某列的unique值

代码语言:R
复制
count(test, Species)

4. use dplyr to deal with related datasets, combining two tables

Two tables:

代码语言:R
复制
test1 <- data.frame(x = c('b','e','f','x'), 
                    z = c("A","B","C",'D'))
test1
test2 <- data.frame(x = c('a','b','c','d','e','f'), 
                    y = c(1,2,3,4,5,6))
test2 

(1) inner_join

代码语言:R
复制
inner_join(test1, test2, by = "x")  #內连取交集, 不能匹配的cases丢弃

(2) left_join

代码语言:R
复制
left_join(test1, test2, by = 'x') # 左连,以前面左边这个test1的x为准,把test2的内容匹配过去,不匹配的cases丢弃
left_join(test2, test1, by = 'x') # 左连,以前边左边这个test2的x为准,把test的呢内容匹配进去,确实数值用NA,不匹配的丢弃

(3) full_join

代码语言:R
复制
left_join(test2, test1, by = 'x') # 全连, 把test 1,test2 以x为准合并,长表变短表

(4) semi_join

代码语言:R
复制
semi_join(x = test1, y = test2, by = 'x') #半连接, 返回能够与y表匹配的x表所有记录,不合并两表格,只针对x操作

(5) 反连接:返回无法与y表匹配的x表的所记录anti_join

代码语言:R
复制
anti_join(x = test2, y = test1, by = 'x') # 反向半连接,返回不能够与y表匹配的x表所有记录,不合并两表格,只针对x操作

(6) 简单合并

bind_rows() # 两表列数相同,行+行 简单纵扩

bind_cols() # 两表行数相同,列+列 简单横扩

代码语言:R
复制
test1 <- data.frame(x = c(1,2,3,4), y = c(10,20,30,40))
test1
test2 <- data.frame(x = c(5,6), y = c(50,60))
test2
test3 <- data.frame(z = c(100,200,300,400))
test3
bind_rows(test1, test2)
bind_cols(test1, test3)
bind_rows(test1,test3) #列数不同,报错么?
bind_cols(test2,test3) #行数不同,如何?

5. how to learn R packages

(1) check help document by ?

代码语言:R
复制
?sd

(2) search the introduction of this R package

(3) Vignettes, a brief tutorial drafted by the author

example

代码语言:R
复制
browseVignettes("limma")

The end

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. installation and loading
    • 1.1 choose a mirro to download R packages
      • 1.2 install
      • 2. functions in dplyr
      • 3. two useful skills of dplyr
      • 4. use dplyr to deal with related datasets, combining two tables
      • 5. how to learn R packages
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档