R合并由字符串添加副本



我第一次尝试使用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,类似于上面的例子。

交换数据帧和左连接的顺序。

这是你的数据集的精简版本:

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>

重要的洞察力是mpnamesconstituencies数据集中是唯一的,而不是combine数据集。

最新更新