Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将两个变量与另一数据帧中的一组变量合并/匹配_R_Merge_Dataframe_Match_Plyr - Fatal编程技术网

R 将两个变量与另一数据帧中的一组变量合并/匹配

R 将两个变量与另一数据帧中的一组变量合并/匹配,r,merge,dataframe,match,plyr,R,Merge,Dataframe,Match,Plyr,我有两个data.framedf.1和df.2,我将合并或从中选择数据以创建新的data.framedf.1包含关于每个个体(ID)、采样事件(event)、Site和样本编号(sample)的信息。对我来说,棘手的部分是站点和每个ID-事件配对对应的样本是不同的。例如,F3-3有站点“梅花”表示样本“1”,M6-3有站点“梨”表示样本“1” df.2具有Sample1和Sample2,它们通过ID-事件配对对应于df.1中的Sample信息 我想在这两个data.frames之间匹配/合并

我有两个data.frame
df.1
df.2
,我将合并或从中选择数据以创建新的data.frame
df.1
包含关于每个个体(
ID
)、采样事件(
event
)、
Site
和样本编号(
sample
)的信息。对我来说,棘手的部分是
站点
和每个
ID
-
事件
配对对应的
样本
是不同的。例如,F3-3有
站点
“梅花”表示
样本
“1”,M6-3有
站点
“梨”表示
样本
“1”

df.2
具有
Sample1
Sample2
,它们通过
ID
-
事件配对对应于
df.1
中的
Sample
信息

我想在这两个data.frames之间匹配/合并信息。基本上,从
df.1
中的
站点
获取与
样本
编号匹配的“单词”。下面是一个示例(
df.3

每个
ID
-
事件
配对将只有一个
站点
和相应的
样本
(例如,“苹果”将对应于“1”,而不是“1”和“4”)。我知道如果我只是匹配,我可以使用
merge
,例如,
Sample1
Sample2
我不知道如何使用这两个词来填充
Site1
Site2

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))

df.1Two
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
首先