3

我需要合并两个数据框。第一个看起来像这样:

> df1 <- data.frame(Artist = c("Vincent van ", "Vincent van ", "Theo van Gogh", "Alexandre", "Alexandre"), Location = c("a","a","a","b","c"), time = c(1,2,1,1,1))
> df1
         Artist Location time
1  Vincent van         a    1
2  Vincent van         a    2
3 Theo van Gogh        a    1
4     Alexandre        b    1
5     Alexandre        c    1

第二个:

> df2 <- data.frame(Artist = c("Vincent van Gogh", "Theo van Gogh", "Alexandre Dumas", "Alexandre Dumas"), HomeNumber = c(123,234,456,789), Location = c( "a","a","b","c"))
> df2
            Artist HomeNumber Location
1 Vincent van Gogh        123        a
2    Theo van Gogh        234        a
3  Alexandre Dumas        456        b
4  Alexandre Dumas        789        c

我想要这个数据框:

> df3 <- data.frame(Artist = c("Vincent van ", "Vincent van ", "Theo van Gogh", "Alexandre", "Alexandre"), Location = c("a","a","a","b","c"), time = c(1,2,1,1,1), HomeNumber = c(123,123,234,456,789))
> df3
         Artist Location time HomeNumber
1  Vincent van         a    1        123
2  Vincent van         a    2        123
3 Theo van Gogh        a    1        234
4     Alexandre        b    1        456
5     Alexandre        c    1        789
> 

合并仅适用于 Theo:

    > df3 <- merge(df1, df2, by.x = "Artist", by.y = "Artist", all.x =TRUE)
> df3
         Artist Location.x time HomeNumber Location.y
1     Alexandre          b    1         NA       <NA>
2     Alexandre          c    1         NA       <NA>
3 Theo van Gogh          a    1        234          a
4  Vincent van           a    1         NA       <NA>
5  Vincent van           a    2         NA       <NA> 

原因有两个: (a) 文森特在df1. (b) 亚历山大是大仲马和大仲马的名字。

我可以用 处理(a)df1$Artist <- gsub("Vincent van $","Vincent van Gogh", df1$Artist),但我的数据实际上非常大,在执行之前gsub我必须先知道文森特的全名。一种可能的解决方案是grep("Vincent van "...在 df2 中使用,构建一个函数,如果结果向量的长度是1我将使用gsub返回df2$Artist的 to df1。我不知道该怎么做。

(b) 对我来说有点棘手。我能想到的一个解决方案(但不能编码)是首先使用一个if函数从一个位置选择 Alexandre,然后使用 (a) 的解决方案来gsub命名。

我认为解决 (a) 和 (b) 会返回我想要df3的 . 你们知道如何有效地合并这些数据框吗?谢谢!

编辑:请注意,这Alexandre实际上是两个不同的单位。因此,当合并两者时,应该有它们关联的 HomeNumber 和 Location。Vincent是一个单一的单位,但在时间上有两个观察。

4

1 回答 1

2

您希望该结果被以下事实破坏:您必须在每个数据框中有两行您想要考虑为具有相同的 id,即Alexandre行。JOIN 过程将使其成为 2 x 2 匹配:

df2$short <- substr(df2$Artist, 1,7)
df1$short <- substr(df1$Artist, 1,7)
(dfmer <- merge(df1, df2, by="short") )
#-----
    short      Artist.x Location.x time         Artist.y HomeNumber Location.y
1 Alexand     Alexandre          b    1  Alexandre Dumas        456          b
2 Alexand     Alexandre          b    1  Alexandre Dumas        789          c
3 Alexand     Alexandre          c    1  Alexandre Dumas        456          b
4 Alexand     Alexandre          c    1  Alexandre Dumas        789          c
5 Theo va Theo van Gogh          a    1    Theo van Gogh        234          a
6 Vincent  Vincent van           a    1 Vincent van Gogh        123          a
7 Vincent  Vincent van           a    2 Vincent van Gogh        123          a

如果您想挑选第一个实例,您可以使用 !duplicated on location 和 time:

> dfmer[!duplicated( dfmer[, c("Location.x", "time")]), ]
    short      Artist.x Location.x time         Artist.y HomeNumber Location.y
1 Alexand     Alexandre          b    1  Alexandre Dumas        456          b
3 Alexand     Alexandre          c    1  Alexandre Dumas        456          b
5 Theo va Theo van Gogh          a    1    Theo van Gogh        234          a
7 Vincent  Vincent van           a    2 Vincent van Gogh        123          a

为了回应关注(以前没有提出需要将 Location 添加为链接变量:

> (dfmer <- merge(df1, df2, by=c("short", "Location") ) )
    short Location      Artist.x time         Artist.y HomeNumber
1 Alexand        b     Alexandre    1  Alexandre Dumas        456
2 Alexand        c     Alexandre    1  Alexandre Dumas        789
3 Theo va        a Theo van Gogh    1    Theo van Gogh        234
4 Vincent        a  Vincent van     1 Vincent van Gogh        123
5 Vincent        a  Vincent van     2 Vincent van Gogh        123
于 2013-04-23T17:39:42.610 回答