2015-04-19 29 views
0

我有兩個數據集,都包含「ID」變量。在這兩個數據集中,我如何保留這兩個數據集中存在的ID?我使用R.我如何保留兩個數據中都存在「ID」的觀察結果?


df1 <- structure(list(CustomerId = c(1, 2, 3, 4, 5, 8, 9), Product = structure(c(4L, 
4L, 4L, 3L, 3L, 1L, 2L), .Label = c("abc", "def", "Radio", "Toaster" 
), class = "factor")), .Names = c("CustomerId", "Product"), row.names = c(NA, 
-7L), class = "data.frame") 
df2 <- 
structure(list(CustomerId = c(2, 4, 6, 7), State = structure(c(2L, 
2L, 3L, 1L), .Label = c("aaa", "Alabama", "Ohio"), class = "factor")), .Names = c("CustomerId", 
"State"), row.names = c(NA, -4L), class = "data.frame") 

在兩組數據,我想留同時存在於兩個數據的觀察。 (這些將是ID 2和4兩個數據集。)

+0

你希望的輸出是什麼?兩個新的數據框與另一個ID存在的行?這將有助於獲得一個[可重現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)以及樣本輸入和期望的輸出。 – MrFlick

+0

我編輯過。你能弄清楚嗎? –

回答

0

你可以只使用基本的子集就像

subset(df1, CustomerId %in% df2$CustomerId) 
subset(df2, CustomerId %in% df1$CustomerId) 
的,如果你使用 dplyr

被稱爲semi_join

library(dplyr) 
semi_join(df1, df2) 
semi_join(df2, df1) 
+0

非常感謝 –

0

合併()將是最簡單的解決方案之一。這是內連接等效,並檢查其他參數是否需要外連接。

merge(df1, df2, by="CustomerId")[,1:2] 
    CustomerId Product 
1   2 Toaster 
2   4 Radio 

merge(df2, df1, by="CustomerId")[,1:2] 
    CustomerId State 
1   2 Alabama 
2   4 Alabama