Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 - Fatal编程技术网

R 合并包含重复元素的两个数据帧

R 合并包含重复元素的两个数据帧,r,merge,R,Merge,给定两个名称部分重叠的数据帧,foo和bar: foo <- iris[1:10,-c(4,5)] # Sepal.Length Sepal.Width Petal.Length # 1 5.1 3.5 1.4 # 2 4.9 3.0 1.4 # 3 4.7 3.2 1.3 # 4 4.6

给定两个名称部分重叠的数据帧,
foo
bar

foo <- iris[1:10,-c(4,5)]
#   Sepal.Length Sepal.Width Petal.Length
# 1           5.1         3.5          1.4
# 2           4.9         3.0          1.4
# 3           4.7         3.2          1.3
# 4           4.6         3.1          1.5
# 5           5.0         3.6          1.4
# 6           5.4         3.9          1.7
# 7           4.6         3.4          1.4
# 8           5.0         3.4          1.5
# 9           4.4         2.9          1.4
# 10          4.9         3.1          1.5

bar <- iris[3:13,-c(3,5)]
bar[1:8, ] <- bar[1:8, ] * 2
#    Sepal.Length Sepal.Width Petal.Width
# 3           9.4         6.4         0.4
# 4           9.2         6.2         0.4
# 5          10.0         7.2         0.4
# 6          10.8         7.8         0.8
# 7           9.2         6.8         0.6
# 8          10.0         6.8         0.4
# 9           8.8         5.8         0.4
# 10          9.8         6.2         0.2
# 11          5.4         3.7         0.2
# 12          4.8         3.4         0.2
# 13          4.8         3.0         0.1
但是,它会为组成数据帧中的每一列创建一个不同的列,而不管它们是否共享名称

所需的输出如下:

#    Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1           5.1         3.5          1.4          NA # unique to foo
# 2           4.9         3.0          1.4          NA # unique to foo
# 3           9.4         6.4          1.3          0.4 # overlap, retained from bar
# 4           9.2         6.2          1.5          0.4 # 
# 5          10.0         7.2          1.4          0.4 # .
# 6          10.8         7.8          1.7          0.8 # .
# 7           9.2         6.8          1.4          0.6 # .
# 8          10.0         6.8          1.5          0.4 # 
# 9           8.8         5.8          1.4          0.4 # 
# 10          9.8         6.2          1.5          0.2 # overlap, retained from bar
# 11          5.4         3.7           NA          0.2 # unique to bar
# 12          4.8         3.4           NA          0.2 # unique to bar
# 13          4.8         3.0           NA          0.1 # unique to bar
我的直觉是将数据子集为两个不相交的集合,以及
栏中相交元素的集合,然后将它们合并,但我相信有一个更优雅的解决方案

(编辑) plyr的包装对于这类东西来说太棒了。只要做:

 library(plyr)
 foo$ID <- row.names(foo)
 bar$ID <- row.names(bar)
 foobar <- join(foo, bar, type = "full", by = "ID")
库(plyr)

foo$ID我看到了关于
plyr::join
的热情洋溢的推荐,但没有看到它与基本
合并所提供的有多大不同:

 merge(foo, bar, by=c("Sepal.Length", "Sepal.Width"), all=TRUE)

错误
[.data.frame
(x,by):选择了未定义的列
。此外,帮助页面建议我们应该期望结果与
合并
中的结果相同。现在这并不像OP所希望的那样进行覆盖。请测试并与他预期的输出进行比较。啊,我明白了……是的,我认为我所使用的任何解决方案都不会比voidHead更好正在考虑加入(bar,foo,type=“full”,by=“ID”,match=“first”)
似乎更像是这样。如果OP不关心行和列的顺序。那么,这显然不是OP想要的。只需将您的输出与OP的进行比较。同意不清楚。我假设花瓣宽度值的差异是由OP部分的惰性造成的。缺少的计算文本值是由l解释的aziness在我这方面。@BondedDust您指的是哪一个Petal.Width值?我手工构造了预期的输出,但我相信它与示例数据一致。所有那些Petal.Length值都小于1.0。原始中没有这样的值。对。您已更正。
 merge(foo, bar, by=c("Sepal.Length", "Sepal.Width"), all=TRUE)