2014-04-01 46 views
0

我有兩個數據框架df.1df.2,我將合併或以其他方式選擇數據以創建新的data.frame。 df.1包含關於每個人(ID),採樣事件(Event),Site和樣本編號(Sample)的信息。對我來說棘手的部分是Site和相應的Sample對於每個ID-Event配對是不同的。例如,對於Sample「1」,F3-3具有Site「梅花」,對於Sample「1」具有Site「梨」。將兩個變量與另一個數據幀的一組變量合併/匹配

data.frame df.1

df.2Sample1Sample2其中由ID的方式對應於Sample信息df.1 - Event配對。

data.frame df.2

我想匹配/合併這兩個data.frames之間的信息。本質上,從Site中獲取df.1中與Sample編號匹配的「單詞」。下面是一個例子(df.3)。

data.frame df.3

每個ID - Event配對只會有一個Site和對應Sample(例如, 「蘋果」 將對應於 「1」,不爲 「1」 和 「4」)。我知道我可以使用merge,如果我只匹配,例如,Sample1Sample2我不知道如何做到這一點與填寫Site1Site2與正確匹配的單詞。

df.1 <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("F1", 
"F3", "M6"), class = "factor"), Sex = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("F", "M"), class = "factor"), Event = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L), Site = structure(c(1L, 3L, 9L, 7L, 8L, 10L, 
2L, 6L, 4L, 5L, 1L, 9L, 7L, 8L, 10L, 5L, 10L, 2L, 6L, 4L, 5L, 
1L, 9L, 2L, 6L, 4L, 5L, 1L, 8L, 3L, 10L, 4L, 2L, 6L, 4L, 5L, 
1L), .Label = c("Apple", "Banana", "Grape", "Guava", "Kiwi", 
"Mango", "Orange", "Peach", "Pear", "Plum"), class = "factor"), 
    Sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
    3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L)), .Names = c("ID", 
"Sex", "Event", "Site", "Sample"), class = "data.frame", row.names = c(NA, 
-37L)) 
# 
df.2 <- structure(list(Sample1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L), Sample2 = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
3L, 4L, 5L), V1 = c(0.12, 0.497, 0.715, 0, 0.001, 0, 0.829, 0, 
0, 0.001, 0, 0.829), V2 = c(0.107, 0.273, 0.595, 0, 0.004, 0, 
0.547, 0.001, 0.001, 0.107, 0.273, 0.595), ID = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("F1", 
"M6"), class = "factor"), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("F", "M"), class = "factor"), 
    Event = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L)), .Names = c("Sample1", 
    "Sample2", "V1", "V2", "ID", "Sex", "Event"), class = "data.frame", row.names = c(NA, 
    -12L)) 
# 
df.3 <- structure(list(Sample1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L), Sample2 = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
3L, 4L, 5L), V1 = c(0.12, 0.497, 0.715, 0, 0.001, 0, 0.829, 0, 
0, 0.001, 0, 0.829), V2 = c(0.107, 0.273, 0.595, 0, 0.004, 0, 
0.547, 0.001, 0.001, 0.107, 0.273, 0.595), Site1 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Apple", 
"Banana"), class = "factor"), Site2 = structure(c(2L, 8L, 6L, 
7L, 9L, 1L, 5L, 3L, 4L, 5L, 3L, 4L), .Label = c("Banana", "Grape", 
"Guava", "Kiwi", "Mango", "Orange", "Peach", "Pear", "Plum"), class = "factor"), 
ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L), .Label = c("F1", "M6"), class = "factor"), Sex = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("F", 
"M"), class = "factor"), Event = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 3L)), .Names = c("Sample1", "Sample2", 
"V1", "V2", "Site1", "Site2", "ID", "Sex", "Event"), class = "data.frame", row.names = c(NA, -12L)) 

回答

1

兩個merge S的關係做:

first <- merge(df.2, unique(df.1[,3:5]), by.x=c("Sample1","Event"), by.y=c("Sample","Event"), all.x=TRUE) 
second <- merge(first, unique(df.1[,3:5]),by.x=c("Sample2","Event"), by.y=c("Sample","Event"), all.x=TRUE) 

print(second) 
    Sample2 Event Sample1 V1 V2 ID Sex Site.x Site.y 
1  10  1  1 0.000 0.001 F1 F Apple Kiwi 
2  2  1  1 0.120 0.107 F1 F Apple Grape 
3  3  1  1 0.497 0.273 F1 F Apple Pear 
4  3  3  2 0.001 0.107 M6 M Banana Mango 
5  4  1  1 0.715 0.595 F1 F Apple Orange 
6  4  3  2 0.000 0.273 M6 M Banana Guava 
7  5  1  1 0.000 0.000 F1 F Apple Peach 
8  5  3  2 0.829 0.595 M6 M Banana Kiwi 
9  6  1  1 0.001 0.004 F1 F Apple Plum 
10  7  1  1 0.000 0.000 F1 F Apple Banana 
11  8  1  1 0.829 0.547 F1 F Apple Mango 
12  9  1  1 0.000 0.001 F1 F Apple Guava 
相關問題