加入数据框时追加和覆盖 [英] Appending and overwriting when joining dataframes

查看:410
本文介绍了加入数据框时追加和覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个数据框:

prim <- data.frame("t"=2007:2012,
                   "a"=1:6,
                   "b"=7:12)

secnd <- data.frame("t"=2012:2013,
                    "a"=c(5, 7))

third <- data.frame("t"=2012:2013,
                    "b"=c(11, 13))

我想分两个步骤将secndthird联接到prim.在第一步中,我加入了primsecnd,其中prim中的任何现有元素都被secnd中的元素覆盖,所以我们得出以下结论:

I want to join secnd and third to prim in two steps. In the first step I join prim and secnd, where any existing elements in prim are overwritten by those in secnd, so we end up with:

     t  a  b
1 2007  1  7
2 2008  2  8
3 2009  3  9
4 2010  4 10
5 2011  5 11
6 2012  5 12
7 2013  7 NA

此后,我想加入third,在这里,现有元素再次被third中的元素覆盖:

After this I want to join with third, where again existing elements are overwritten by those in third:

     t  a  b
1 2007  1  7
2 2008  2  8
3 2009  3  9
4 2010  4 10
5 2011  5 11
6 2012  5 11
7 2013  7 13

是否有一种方法可以使用dplyr或基数R实现?

Is there a way to achieve this using dplyr or base R?

推荐答案

通过使用dplyr,您可以执行以下操作:

By using dplyr you can do:

require(dplyr)

prim %>% full_join(secnd, by = 't') %>%
  full_join(third, by = 't') %>%
  mutate(a = coalesce(as.integer(a.y),a.x),
         b = coalesce(as.integer(b.y),b.x)) %>%
  select(t,a,b)

我添加了as.integer函数,因为数据框中的数据类型不同.

I added the as.integer function since you have different data types in your dataframes.

这篇关于加入数据框时追加和覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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