合并两个数据框,同时保持原始行顺序 [英] Merge two data frames while keeping the original row order

查看:19
本文介绍了合并两个数据框,同时保持原始行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并两个数据框,保持其中一个的原始行顺序(在下面的示例中为df.2).

I want to merge two data frames keeping the original row order of one of them (df.2 in the example below).

以下是一些示例数据(class 列中的所有值都在两个数据框中定义):

Here are some sample data (all values from class column are defined in both data frames):

df.1 <- data.frame(class = c(1, 2, 3), prob = c(0.5, 0.7, 0.3))
df.2 <- data.frame(object = c('A', 'B', 'D', 'F', 'C'), class = c(2, 1, 2, 3, 1))

如果我这样做:

merge(df.2, df.1)

输出为:

  class object prob
1     1      B  0.5
2     1      C  0.5
3     2      A  0.7
4     2      D  0.7
5     3      F  0.3

如果我添加sort = FALSE:

merge(df.2, df.1, sort = F)                                                        

结果是:

  class object prob
1     2      A  0.7
2     2      D  0.7
3     1      B  0.5
4     1      C  0.5
5     3      F  0.3

但我想要的是:

  class object prob
1     2      A  0.7
2     1      B  0.5
3     2      D  0.7
4     3      F  0.3    
5     1      C  0.5

推荐答案

查看plyr包中的join函数.这就像合并,但它允许您保持其中一个数据集的行顺序.总的来说,它比合并更灵活.

Check out the join function in the plyr package. It's like merge, but it allows you to keep the row order of one of the data sets. Overall, it's more flexible than merge.

使用您的示例数据,我们将像这样使用 join:

Using your example data, we would use join like this:

> join(df.2,df.1)
Joining by: class
  object class prob
1      A     2  0.7
2      B     1  0.5
3      D     2  0.7
4      F     3  0.3
5      C     1  0.5

这里有几个链接描述了对合并函数的修复以保持行顺序:

Here are a couple of links describing fixes to the merge function for keeping the row order:

http://www.r-statistics.com/2012/01/merging-two-data-frame-objects-while-preserving-the-rows-order/

http://r.789695.n4.nabble.com/patching-merge-to-allow-the-user-to-keep-the-order-of-两个数据帧对象之一合并-td4296561.html

这篇关于合并两个数据框,同时保持原始行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆