2017-06-19 81 views
0

我想在R中加入兩個data.tables我按照名稱加入它們,我想將一個數據表中的行插入到另一個數據表的名稱組中。數據表B具有「名稱」和「數量」,數據表B具有「名稱」和「地址」(但每個名稱不止一個地址)。我想要一個數據表,其中包含每個名稱,相應的地址以及每個名稱組的一個「金額」。在加入時將行插入組

我試過在dplyr中使用「left_join」,但是對於每個「地址」行,數量列都會得到重複。

任何人有任何想法?謝謝。

示例圖片(接合表1和2以創建3):

甚至像這樣:

編輯:添加的兩個數據集是什麼可再現的例子喜歡和期望的輸出是什麼

table_one <- data.frame(name=c("x","y","z"), amount=c("$100","200","300")) 
table_two <- data.frame(name=c("x","x","y","z","z","z"), address=c("A","B","C","D","E","F")) 

output <- data.frame(name=c("x","x","y","z","z","z"), 
        address=c("A","B","C","D","E","F"), amount=c("$100","","$200","$300","","")) 
+2

最好是包括[重複的例子]做到這一點(https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)樣本輸入數據的形式,我們可以複製/粘貼。數據圖片沒有幫助。 – MrFlick

+0

表3看起來更像是行綁定而不是加入我。也許'bind_rows'? – aosmith

回答

1

使用dplyr

library(dplyr) 

left_join(table_two, table_one, by = 'name') %>% 
    mutate(amount = replace(amount, duplicated(name), NA)) 
# name address amount 
#1 x  A $100 
#2 x  B <NA> 
#3 y  C 200 
#4 z  D 300 
#5 z  E <NA> 
#6 z  F <NA> 
0

在這裏,你去。

table_one <- data.frame(name=c("x","y","z"), amount=c("$100","$200","$300")) 
table_two <- data.frame(name=c("x","x","y","z","z","z"), address=c("A","B","C","D","E","F")) 

output <- data.frame(name=c("x","x","y","z","z","z"), 
        address=c("A","B","C","D","E","F"), amount=c("$100","","$200","$300","","")) 


test <- merge(table_one, table_two, by = 'name') 
test$amount <- as.character(test$amount) 
test$amount[duplicated(test[,c(1,2)])] <- "" 
test 
0

我們可以match

i1 <- with(table_one, match(name, table_two$name)) 
table_two$amount <- "" 
table_two$amount[i1] <- as.character(table_one$amount) 
table_two 
# name address amount 
#1 x  A $100 
#2 x  B  
#3 y  C 200 
#4 z  D 300 
#5 z  E  
#6 z  F