合并 2 个数据帧,丢弃不匹配的行 [英] Merge 2 data frames, discard unmatched rows

查看:51
本文介绍了合并 2 个数据帧,丢弃不匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框——一个很大(超过 200 万行),一个较小(大约 300,000 行).较小的数据帧是较大数据帧的子集.唯一的区别是较大的有一个额外的属性,我需要添加到较小的.

I have two data frames--one is huge (over 2 million rows) and one is smaller (around 300,000 rows). The smaller data frame is a subset of the larger one. The only difference is that the larger one has an additional attribute that I need to add to the smaller one.

具体来说,大数据框的属性是(日期、时间、地址、标志),小数据框的属性是(日期、时间、地址).我需要以某种方式将正确的相应 Flag 值放入每行的较小数据框中.合并"的数据帧的最终大小应该与我较小的相同,丢弃大数据帧中未使用的行.

Specifically, the attributes for the large data frame are (Date, Time, Address, Flag) and the attributes for the small data frame are (Date, Time, Address). I need to get the correct corresponding Flag value somehow into the smaller data frame for each row. The final size of the "merged" data frame should be the same as my smaller one, discarding the unused rows from the large data frame.

实现这一目标的最佳方法是什么?

What is the best way to accomplish this?

更新:我使用以下内容测试了合并功能:

Update: I tested the merge function with the following:

new<-merge(data12, data2, by.x = c("Date", "Time", "Address"), 
           by.y=c("Date", "Time", "Address"))

new<-merge(data12, data2, by = c("Date", "Time", "Address"))

两者都返回一个空数据框(新)和正确数量的属性以及以下警告消息:

both return an empty data frame (new) with the right number of attributes as well as the following warning message:

Warning message:In `[<-.factor`(`*tmp*`, ri, value = c(15640, 15843, 15843, 15161,  : invalid factor level, NAs generated

推荐答案

    R> df1 = data.frame(a = 1:5, b = rnorm(5))
    R> df1
      a           b
    1 1 -0.09852819
    2 2 -0.47658118
    3 3 -2.14825893
    4 4  0.82216912
    5 5 -0.36285430
    R> df2 = data.frame(a = 1:10000, c = rpois(10000, 6))
    R> head(df2)
      a c
    1 1 2
    2 2 4
    3 3 5
    4 4 3
    5 5 3
    6 6 8
    R> merge(df1, df2)
      a           b c
    1 1 -0.09852819 2
    2 2 -0.47658118 4
    3 3 -2.14825893 5
    4 4  0.82216912 3
    5 5 -0.36285430 3

这篇关于合并 2 个数据帧,丢弃不匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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