前言
今天是R学习的第三天,继续。
设置镜像站
file.edit('~/.Rprofile')
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #对应清华源
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") #对应中科大源
设置好之后,点保存,然后重启Rstuio看看,貌似成功了。
> options()$BioC_mirror
[1] "https://mirrors.ustc.edu.cn/bioc/"
安装包
install.packages(“包名”)或者BiocManager::install(“包名”),前者为CRAN包,后者为bioconductor包。
install.packages("dplyr")
加载包
library(包)或require(包)均可。
library(dplyr)
dplyr包操作练习
新增列
> test <- iris[c(1:2,51:52,101:102),] #取iris数据的第1,2,51,52,101,102行
> mutate(test, new = Sepal.Length * Sepal.Width) #新增列名为new,值为Sepal.Length * Sepal.Width
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66
按列筛选
按列号筛选
select(test,1) #取test数据第1列
select(test,c(1,5)) #取test数据第1列和第5列
按列名筛选
select(test,Sepal.Length) #取 Sepal.Length列
select(test, Petal.Length, Petal.Width) #取Petal.Length和Petal.Width列
vars <- c("Petal.Length", "Petal.Width") #设置Petal.Length和Petal.Width为vars数据集
select(iris, one_of(vars)) #取vars中的数据库
filter函数筛选行
filter(test, Species == "setosa") #筛选species数值为setosa的行
filter(test, Species == "setosa"&Sepal.Length > 5 ) #筛选species数值为setosa的行且sepal.length值大于5的行
filter(test, Species %in% c("setosa","versicolor"))#筛选species数值为setosa和versicolor的行
arrange函数排序表格
arrange(test, Sepal.Length)#默认Sepal.Length从小到大排序
arrange(test, desc(Sepal.Length))#用desc Sepal.Length从大到小排序
summarise函数汇总
summarise(test, mean(Sepal.Length), sd(Sepal.Length)) # 计算Sepal.Length的平均值和标准差
group_by(test, Species)#按照species列进行分组
summarise(group_by(test, Species),mean(Sepal.Length), sd(Sepal.Length))#按照species列进行分组,计算分组后不同组别的Sepal.Length的平均值和标准差
管道操作
英文输入法下输入ctrl+shift+M直接打出管道符号%>%,其意思是将%>%左边的对象传递给右边的函数,这样可以少打一些字,据说当数据量大的时候可以节省计算内存。
test %>%
group_by(Species) %>%
summarise(mean(Sepal.Length), sd(Sepal.Length)) #取test数据集,按照species列进行分组,计算分组后不同组别的Sepal.Length的平均值和标准差。
count函数统计
count(test,Species) #统计test数据中species列每个值有多少个。
inner_join函数取交集
options(stringsAsFactors = F) #避免系统自动将数据框内的相关变量转换成因子
test1 <- data.frame(x = c('b','e','f','x'), z = c("A","B","C",'D'),stringsAsFactors = F) #创建数据框test1
test2 <- data.frame(x = c('a','b','c','d','e','f'), y = c(1,2,3,4,5,6),stringsAsFactors = F) #创建数据框test2
inner_join(test1, test2, by = "x") # 按第x列相同值取test1和test2的值重新组成数据框
##left_join函数
left_join(test1, test2, by = 'x') #此时基于x的连接只保留了test1对应的x值的数据,当test2相应的值不存在的时候,用NA代替;
x z y
1 b A 2
2 e B 5
3 f C 6
4 x D NA
left_join(test2, test1, by = 'x') #此时基于x的连接只保留了test2对应的x值的数据,当test1相应的值不存在的时候,用NA代替;
x y z
1 a 1 <NA>
2 b 2 A
3 c 3 <NA>
4 d 4 <NA>
5 e 5 B
6 f 6 C
full_join函数全连接
full_join( test1, test2, by = 'x') #此时基于x的连接保留了所有x值对应的数据,当相应的值不存在的时候,用NA代替;
x z y
1 b A 2
2 e B 5
3 f C 6
4 x D NA
5 a <NA> 1
6 c <NA> 3
7 d <NA> 4
半连接函数semi_join
semi_join(x = test1, y = test2, by = 'x') #返回能够与y表匹配的x表所有记录
x z
1 b A
2 e B
3 f C
反连函数anti_join
anti_join(x = test2, y = test1, by = 'x') #返回无法与y表匹配的x表的所记录
x y
1 a 1
2 c 3
3 d 4
简单合并
注意:bind_rows()函数需要两个表格列数相同,而bind_cols()函数则需要两个数据框有相同的行数
bind_rows(test1, test2) #test1,test2数据按行合并
bind_cols(test1, test3) #test1,test3数据按列合并
后记
今天的内容虽然基本上只有一个数据处理包dplyr包的操作,但初次这么详细、这么多函数的操作理解起来稍微有点费劲,还需要要实践中运用,然后积累。