我第一次尝试使用merge
命令时遇到了以下我似乎无法解决的问题。我有两个数据框,我试图使用一列(一串人的名字)合并。当我合并时,结果会导致某些名称正确合并,但大多数名称不合并只添加额外的空行。我的目标是在合并数据帧之后,与dataframe1(118,663)中的最初观察次数相同,而不是让10个变量有12个(填写所有信息并且不存在NA值)。
虽然我理解对某些人来说这个问题看起来可能类似于讨论合并(内部,外部,左侧或右侧)的其他问题,但我的观点是两倍。 1.帮助解决我几天来一直试图解决的问题。 2.获得帮助因为其他讨论合并并使用前面提到的四种类型的答案并没有清楚地解释这些类型是什么以及它们是如何工作的。
起初,我认为这两个数据框之间的名称拼写错误,或者名字之前或之后有一个额外的空格,这是愚蠢的事情。我已经检查了R中的打印结果并打开了两个cvs文件,并且可以验证名称是否完全相同。
以下是有关数据帧的一些基本信息。
df1(combine)
str(combine)
'data.frame': 118663 obs. of 10 variables:
$ uri: Factor w/ 118163 levels "http://data.parliament.uk/resources/532864",..: 392 393 394 395 396 397 398 399 400 401 ...
$ answer.date : Factor w/ 470 levels "2016-07-07","2016-07-11",..: 5 5 5 5 5 4 4 4 4 4 ...
$ answering.body : Factor w/ 33 levels "Cabinet Office",..: 8 8 8 8 8 8 8 8 8 8 ...
$ date.tabled : Factor w/ 543 levels "2016-07-05","2016-07-06",..: 5 5 5 5 5 5 5 5 5 5 ...
$ question.text : Factor w/ 117729 levels "To ask Mr Chancellor of the Exchequer, how many complaints relating to class 2 national insurance contributions have been recei"| __truncated__,..: 199 234 236 214 212 198 226 193 190 207 ...
$ tabling.member...label: Factor w/ 753 levels "Biography information for Adam Holloway",..: 105 105 105 62 123 9 112 112 112 112 ...
$ tabling.member.printed: Factor w/ 795 levels "Adam Holloway",..: 105 105 105 62 123 9 112 112 112 112 ...
$ title : Factor w/ 118163 levels "House of Commons Tabled Parliamentary Question 2016/17 41835",..: 396 394 395 474 459 432 433 434 435 436 ...
$ uin : int 42286 42282 42283 42418 42391 42347 42351 42352 42353 42354 ...
$ mpnames : chr "Rachael Maskell" "Rachael Maskell" "Rachael Maskell" "Luciana Berger" ...
和我试图将其与
合并的第二个数据帧df(constituencies)
str(constituencies)
'data.frame': 811 obs. of 3 variables:
$ party : Factor w/ 17 levels "Alliance","Conservative",..: 2 2 8 9 2 9 12 6 2 2 ...
$ constituency: Factor w/ 650 levels "Aberavon","Aberconwy",..: 628 251 614 578 110 40 309 586 482 483 ...
$ mpnames : Factor w/ 811 levels "Adam Afriyie",..: 1 2 4 3 5 6 8 9 10 11 ...
希望很清楚我正在尝试使用mpnames变量合并它们。我开始使用以下代码。正如我所提到的,似乎只适用于一些名字。
combine_constituencies <- merge(combine, constituencies, by = "mpnames", all = TRUE, incomparables = NA)
我根据我在这里看到的有关合并问题的人的建议添加了incomparables = NA
,即使在这种情况下它似乎没有添加任何尚未存在的内容。这次合并的结果给出了正确数量的变量,共12个,但正如您所看到的,观察数量增加了621个。
str(combine_constituencies)
'data.frame': 119284 obs. of 12 variables:
当我查看combine_constituencies
的结果时,我得到了类似于此示例的内容。
mpnames answer.date date.tabled ... party constituency
Zac Goldsmith 2016-04-11 2016-03-23 NA NA
Zac Goldsmith 2016-06-27 2016-06-14 NA NA
Zac Goldsmith NA NA Conservative Richmond Park
我想要获得的结果更接近于此。
mpnames answer.date date.tabled ... party constituency
Zac Goldsmith 2016-04-11 2016-03-23 Conservative Richmond Park
Zac Goldsmith 2016-06-27 2016-06-14 Conservative Richmond Park
如果问题不在于我用来合并的变量,这是我上面提到的几个检查,我不相信,还有什么可能会在这里发生?
为了彻底彻底,我还尝试使用join
,它给出了我所针对的观察总数,但是对于党和选区变量填写了NA,类似于上面的例子。
我们非常感谢任何建议。
答案 0 :(得分:0)
交换数据框的顺序并离开连接。
以下是数据集的简化版本:
constituencies <- data.frame(
mpnames = c("Zac Goldsmith", "Adam Afriyie"),
constituency = c("Aberavon", "Richmond Park"),
party = c("Alliance", "Conservative"),
stringsAsFactors = FALSE
)
combine <- data.frame(
mpnames = c("Zac Goldsmith", "Zac Goldsmith"),
answer.date = as.Date(c("2016-04-11", "2016-06-27")),
date.tabled = as.Date(c("2016-03-23", "2016-06-14")),
stringsAsFactors = FALSE
)
这是您想要的联接:
library(dplyr)
left_join(constituencies, combine, by = "mpnames")
## mpnames constituency party answer.date date.tabled
## 1 Zac Goldsmith Aberavon Alliance 2016-04-11 2016-03-23
## 2 Zac Goldsmith Aberavon Alliance 2016-06-27 2016-06-14
## 3 Adam Afriyie Richmond Park Conservative <NA> <NA>
重要的见解是mpnames
在constituencies
数据集中是唯一的,而在combine
数据集中不是唯一的。