前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >不用SQL,也可以实现数据集的合并和连接

不用SQL,也可以实现数据集的合并和连接

作者头像
1480
发布2019-07-30 15:49:20
1.2K0
发布2019-07-30 15:49:20
举报
文章被收录于专栏:数据分析1480数据分析1480

数据(集)处理是数据分析过程中的重要环节,今天特别整理数据(集)合并、增减与连接的相关内容,并逐一作出示例。

目 录

1 数据合并

1.1 cbind列合并(等长)

1.2 rbind行合并

2 数据连接/匹配

2.1 内连接

2.2 外连接

2.3 左连接

2.4 右连接

2.5 双(多)字段内连接

3 数据增减

正 文

1 数据合并

1.1 cbind列合并(等长)

总结:cbind等行数、按列合并(无序)

代码语言:javascript
复制
#等长
#生成测试数据
> ID1 <- c(1:4)
> ID2 <- c(2:5)
> name<-c("A","B","C","D")
> score<-c(8,22,7,6)
> student1<-data.frame(ID1,name)
> student2<-data.frame(ID2,score)

> student1
  ID1 name
1   1    A
2   2    B
3   3    C
4   4    D
> student2
  ID2 score
1   2     8
2   3    22
3   4     7
4   5     6
> cbind(student1,student2) #按照行合并student1和student2
  ID1 name ID2 score
1   1    A   2     8
2   2    B   3    22
3   3    C   4     7
4   4    D   5     6

1.2 rbind行合并

总结:按行合并,需要注意数据集需要有相同的列字段名

代码语言:javascript
复制
> #生成测试数据student1
> ID <- c(1:4)
> score <- c(8,22,7,33)
> student1<-data.frame(ID,score)
> #生成测试数据student2
> ID <- c("A","B","C","D")
> score <- c(11,2,55,3)
> student2<-data.frame(ID,score)

> student1
  ID score
1  1     8
2  2    22
3  3     7
4  4    33
> student2
  ID score
1  A    11
2  B     2
3  C    55
4  D     3
> rbind(student1,student2) #按行合并,需要注意数据集需要有相同的列字段名
  ID score
1  1     8
2  2    22
3  3     7
4  4    33
5  A    11
6  B     2
7  C    55
8  D     3

2 数据连接/匹配

数据连接主要涉及到merge函数和dplyr包中的*_join等函数,另外sqldf函数(SQL)亦可以实现数据连接功能。本节主要就merge和dplyr::*_join函数做出解释。

merge函数语法

代码语言:javascript
复制
merge(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
      sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE,
      incomparables = NULL, ...)

其中,通过by字段控制连接字段by = "ID"为单字段连接,by = c("ID","NAME",……)为多字段连接;通过all=FALSE/TRUE、all.x = TRUE和all.y = TRUE实现内连接、外连接、左连接和右连接

dplyr包中的inner_join、left_join、right_join、full_join语法

代码语言:javascript
复制
inner_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"),
  ...)
left_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)
right_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"),
  ...)
full_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)

其中,通过by控制连接字段通过dplyr::*_join中的“*”控制连接形式。

代码语言:javascript
复制
> #生成数据集1
> ID<-c(1,2,3)
> name<-c("Jim","Tony","Lisa")
> student1<-data.frame(ID,name)
> #生成数据集1
> ID<-c(1,2,5)
> score<-c(89,22,78)
> student2<-data.frame(ID,score)
> student1
  ID name
1  1  Jim
2  2 Tony
3  3 Lisa
> student2
  ID score
1  1    89
2  2    22
3  5    78

2.1 内连接(指定字段匹配输出)

总结:merge(……,all=FALSE)等价dplyr::inner_join(……)

代码语言:javascript
复制
> #内连接
> merge(student1,student2,by="ID",all=FALSE)
  ID name score
1  1  Jim    89
2  2 Tony    22
> dplyr::inner_join(student1,student2,by="ID")
  ID name score
1  1  Jim    89
2  2 Tony    22

2.2 外连接

总结:merge(……,all=TRUE)等价dplyr::full_join(……)所有数据均加入,如无法连接则生成NA

代码语言:javascript
复制
> #外连接
> merge(student1,student2,by="ID",all=TRUE)
  ID name score
1  1  Jim    89
2  2 Tony    22
3  3 Lisa    NA
4  5 <NA>    78
> dplyr::full_join(student1,student2,by="ID")
  ID name score
1  1  Jim    89
2  2 Tony    22
3  3 Lisa    NA
4  5 <NA>    78

2.3 左连接

总结:merge(……,all.x=TRUE)等价dplyr::left_join(……)所有左侧(x)数据均加入,即使无法连接亦保留

代码语言:javascript
复制
> #左连接
> merge(student1,student2,by="ID",all.x=TRUE)
  ID name score
1  1  Jim    89
2  2 Tony    22
3  3 Lisa    NA
> dplyr::left_join(student1,student2,by="ID")
  ID name score
1  1  Jim    89
2  2 Tony    22
3  3 Lisa    NA

2.4 右连接

总结:merge(……,all.y=TRUE)等价dplyr::right_join(……)所有右侧(y)数据均加入,即使无法连接亦保留

代码语言:javascript
复制
> #右连接
> merge(student1,student2,by="ID",all.y=TRUE)
  ID name score
1  1  Jim    89
2  2 Tony    22
3  5 <NA>    78
> dplyr::right_join(student1,student2,by="ID")
  ID name score
1  1  Jim    89
2  2 Tony    22
3  5 <NA>    78

2.5 双(多)字段内连接

代码语言:javascript
复制
> #生成数据集1
> ID<-c(1,2,3)
> SD <- c(1,2,3)
> name<-c("Jim","Tony","Lisa")
> student1<-data.frame(ID,SD,name)
> #生成数据集1
> ID<-c(1,2,5)
> SD <- c(5,2,3)
> score<-c(89,22,78)
> student2<-data.frame(ID,SD,score)
> student1
  ID SD name
1  1  1  Jim
2  2  2 Tony
3  3  3 Lisa
> student2
  ID SD score
1  1  5    89
2  2  2    22
3  5  3    78
> 
> #多字段内连接
> merge(student1,student2,by=c("ID","SD"),all=FALSE)
  ID SD name score
1  2  2 Tony    22
> dplyr::inner_join(student1,student2,by=c("ID","SD"))
  ID SD name score
1  2  2 Tony    22

3 数据增减

常见如以下不同方法

代码语言:javascript
复制
#方法一:减行数或列数
x=x[,-1] #代表删除x数据集中第一列数据

#方法二:dplyr::mutate
#数值重定义和赋值
#将Ozone列取负数赋值给new,然后Temp列重新计算为(Temp - 32) / 1.8
mutate(airquality, new = -Ozone, Temp = (Temp - 32) / 1.8)

#方法三:subset筛选变量服从某值的子集
subset(airquality, Temp > 80, select = c(Ozone, Temp))

#方法四:rbind和cbind
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据分析1480 微信公众号,前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档