合并数据框只保留一个观察

时间:2015-03-01 14:57:31

标签: r merge

我有两个数据框:来宾和地址。

“来宾”包含人员姓名和相应的人员级别数据。人员按家庭ID进行组织,以便同一家庭中的多个人共享家庭ID,如下所示:

## Guests data frame looks like this:
Family.ID <- c(1,1,1,2,2,3,3,3,4,4)
Names <- c("Person_A", "Person_B", "Person_C", "Person_D", "Person_E",
           "Person_F", "Person_G", "Person_H", "Person_I", "Person_J")
list <- c("A", "A", "A", "A", "A", "B", "B", "B", "C", "C")
guests <- data.frame(Family.ID, Names, list)

“地址”包含具有地址的家庭,也标有家庭ID,如下所示:

## Addresses data frame looks like this:
family <- c("Person A, B, & C", "Person D & E", "Person F, G, & H", "Person I & J")
ID <- c(1,2,3,4)
address <- c("family 1's address", "family 2's address", "family 3's address", "family 4's address")
addresses <- data.frame(ID, family, address)
names(addresses) <- c("Family.ID", "Family", "Address")

我的目标是将变量来宾$ list添加到“地址”。我尝试合并“来宾”和“地址”,如下所示:

both <- merge(guests, addresses, by = "Family.ID")

从合并中,我得到:

  Family.ID Names     list  Family              Address
        1   Person_A    A   Person A, B, & C    family 1's address
        1   Person_B    A   Person A, B, & C    family 1's address
        1   Person_C    A   Person A, B, & C    family 1's address
        2   Person_D    A   Person D & E        family 2's address
        2   Person_E    A   Person D & E        family 2's address
        3   Person_F    B   Person F, G, & H    family 3's address
        3   Person_G    B   Person F, G, & H    family 3's address
        3   Person_H    B   Person F, G, & H    family 3's address
        4   Person_I    C   Person I & J        family 4's address
        4   Person_J    C   Person I & J        family 4's address

但我想要的只是将“list”添加到“地址”中,如下所示:

  Family.ID Family              Address             List
        1   Person A, B, & C    family 1's address  A
        2   Person D & E        family 2's address  A
        3   Person F, G, & H    family 3's address  B
        4   Person I & J        family 4's address  C

在合并之前,我是否需要做一些重塑数据帧的事情?

1 个答案:

答案 0 :(得分:0)

您可merge行{&#34}来自&#34;来宾&#34;删除unique列(Names

后的数据
guests[-2]