学习小组Day6笔记--扬马延

R语言package使用

1. 包的安装和使用

image.png

install.packages(),会自动安装运行包所需的环境
library() 激活包的环境

2. 实例 dpylr

dplyr is a grammar of data manipulation, providing a consistent set of verbs that help you solve the most common data manipulation challenges
参考网站:dplyr

dolyr安装和激活

browseVignettes(package = "dplyr")
library(dplyr)

用内部数据框iris创建新的数据框并处理数据

test <- iris[c(1:2,51:52,101:102),]
mutate(test,new=Sepal.Length*Sepal.Width) #新增列
select(test) #按列筛选
filter(test,Species=="setosa")
filter(test,Species=="setosa"&Sepal.Length>5) #合并两个筛选条件
filter(test,Species %in% c("versicolor","virginica"))
arrange(test,Sepal.Width) #从小到大排列
arrange(test,desc(Sepal.Width)) #从大到小排列

对数据进行统计学分析

summarise(test,median(Sepal.Width),sd(Sepal.Width)) #数据统计汇总,有多种统计学函数,详见F1
group_by(test,Species) #根据某一变量分组
summarise(group_by(test,Species),median(Sepal.Width),sd(Sepal.Width))

一个重要的功能:管道函数%>%
%>% #control+shift+M 管道函数,左边的对象传递给右边,类似于赋值
对几个数据框的处理

x<-seq(1:4)
y<-c("a","b","c","d")
test1<-data.frame(x,y,stringsAsFactors = F)
test2<-data.frame(x=seq(2:5),y=c("a","b","d","f"),stringsAsFactors = F)
inner_join(test1,test2) #取交集
left_join(test2,test1,by="x") #左连(前面的那个数据框)
full_join(test1,test2,by="x") #合并两个数据框
semi_join(test1,test2,by="x") #返回能够与y表匹配的x表所有记录
anti_join(test1,test2,by="x") #返回返回无法与y表匹配的x表的所记录
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容