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.
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.
use libray() or require(), both OK
install.packages("dplyr)
library(dplyr)
using simplified iris as demo data
test <- iris[c(1:2,51:52,101:102),]
View(test)
(1) mutate() # adds new variables that are functions of existing variables
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
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.
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
arrange(test, Sepal.Length) #按照Sepal.Length的值,重排列行,默认从小到大排序
arrange(test, desc(Sepal.Length)) #用desc从大到小
(5) summarise():汇总 reduces multiple values down to a single summary
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))
(1) %>% from the applications in above examples, it is powerful to link diferent steps
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length))
(2) count() 统计(计数)某列的unique值
count(test, Species)
Two tables:
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
inner_join(test1, test2, by = "x") #內连取交集, 不能匹配的cases丢弃
(2) left_join
left_join(test1, test2, by = 'x') # 左连,以前面左边这个test1的x为准,把test2的内容匹配过去,不匹配的cases丢弃
left_join(test2, test1, by = 'x') # 左连,以前边左边这个test2的x为准,把test的呢内容匹配进去,确实数值用NA,不匹配的丢弃
(3) full_join
left_join(test2, test1, by = 'x') # 全连, 把test 1,test2 以x为准合并,长表变短表
(4) semi_join
semi_join(x = test1, y = test2, by = 'x') #半连接, 返回能够与y表匹配的x表所有记录,不合并两表格,只针对x操作
(5) 反连接:返回无法与y表匹配的x表的所记录anti_join
anti_join(x = test2, y = test1, by = 'x') # 反向半连接,返回不能够与y表匹配的x表所有记录,不合并两表格,只针对x操作
(6) 简单合并
bind_rows() # 两表列数相同,行+行 简单纵扩
bind_cols() # 两表行数相同,列+列 简单横扩
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) #行数不同,如何?
(1) check help document by ?
?sd
(2) search the introduction of this R package
(3) Vignettes, a brief tutorial drafted by the author
example
browseVignettes("limma")
The end
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。