【问题标题】:Merging datasets based on more than 1 column in both datasets合并基于两个数据集中超过 1 列的数据集
【发布时间】:2016-08-17 19:19:03
【问题描述】:

我正在尝试按年份和国家/地区合并两个数据集。第一个数据集 (df = GNIPC) 代表 1980-2008 年每个国家的人均国民总收入。

         Country  Year GNIpc
       (chr) (dbl) (dbl)
1    Afghanistan  1990    NA
2    Afghanistan  1991    NA
3    Afghanistan  1992  2010
4    Afghanistan  1993    NA
5    Afghanistan  1994 12550
6    Afghanistan  1995    NA

第二个数据集(df = 制裁)代表从 1946 年至今实施的经济制裁。

      country imposition sanctiontype sanctions_period
      (chr)      (dbl)        (chr)            (chr)
1 Afghanistan       1     1 6 8           1997-2001
2 Afghanistan       1     7               1979-1979
3 Afghanistan       1     4 7             1995-2002
4 Albania           1     2 8             2005-2005
5 Albania           1     7               2005-2006
6 Albania           1     8               2004-2005

我想合并这两个数据集,这样对于每个 GNI 年,我要么在该国实施制裁,要么不实施制裁。对于不在制裁期间的 GNI 年份,该值将为 0,而对于那些不在制裁期间的年份,该值为 1。这就是我希望它看起来的样子:

         Country  Year GNIpc Imposition sanctiontype
           (chr) (dbl) (dbl)   (dbl)        (chr)
1    Afghanistan  1990    NA    0            NA
2    Afghanistan  1991    NA    0            NA
3    Afghanistan  1992  2010    0            NA
4    Afghanistan  1993    NA    0            NA
5    Afghanistan  1994 12550    0            NA
6    Afghanistan  1995    NA    1           4 7 

【问题讨论】:

  • 我不会使用该格式的第二个数据集。如果有人把数据交给我,我会(1)畏缩,(2)开始转换它,以便sanctiontype 和每年sanctions_period 的每个组合都有一行。所以Afganistan 将有五行与sanctiontype = 1,1997-2001 年每一行。
  • 阿富汗 1998 年应该是什么样子?每个制裁期(2)是一行,还是1 4 6 7 8一行?
  • 我做了一个不同的数据集,其中每种制裁类型都有自己的行。在这里,我正在寻找一种方法来确定每个 GNI 年份是否有制裁。回顾过去的制裁类型,我该怎么做?

标签: r merge


【解决方案1】:

一些示例数据:

df1 <- data.frame(country = c('Afghanistan', 'Turkey'), 
                  imposition = c(1, 0), 
                  sanctiontype = c('1 6 8', '4'), 
                  sanctions_period = c('1997-2001', '2003-ongoing')
)

      country imposition sanctiontype sanctions_period
1 Afghanistan          1        1 6 8        1997-2001
2      Turkey          0            4     2012-ongoing

“sanctions_period”列可以用dplyrtidyr转换:

library(tidyr)
library(dplyr)

df.new <- separate(df1, sanctions_period, c('start', 'end'), remove = F) %>% 
  mutate(end = ifelse(end == 'ongoing', '2016', end)) %>% 
  mutate(start = as.numeric(start), end = as.numeric(end)) %>% 
  group_by(country, sanctions_period) %>% 
  do(data.frame(country = .$country, imposition = .$imposition, sanctiontype = .$sanctiontype, year = .$start:.$end))

   sanctions_period     country imposition sanctiontype  year
             <fctr>      <fctr>      <dbl>       <fctr> <int>
1         1997-2001 Afghanistan          1        1 6 8  1997
2         1997-2001 Afghanistan          1        1 6 8  1998
3         1997-2001 Afghanistan          1        1 6 8  1999
4         1997-2001 Afghanistan          1        1 6 8  2000
5         1997-2001 Afghanistan          1        1 6 8  2001
6      2012-ongoing      Turkey          0            4  2012
7      2012-ongoing      Turkey          0            4  2013
8      2012-ongoing      Turkey          0            4  2014
9      2012-ongoing      Turkey          0            4  2015
10     2012-ongoing      Turkey          0            4  2016

从那里,它应该很容易与您的第一个数据框合并。请注意,您的第一个数据框将 Country 和 Year 大写,而第二个则没有。

df.merged <- merge(df.first, df.new, by.x = c('Country', 'Year'), by.y = c('country', 'year'))

【讨论】:

  • 我对我的数据集执行了以下操作,但出现错误:df.new &lt;- separate(sanctions4, sanctions_period, c('start', 'end'), remove = F) %&gt;% mutate(start = as.numeric(start), end = as.numeric(end)) %&gt;% group_by(country, sanctions_period) %&gt;% do(data.frame(country = .$country, imposition = .$imposition, sanctiontype = .$sanctiontype, year = .$start:.$end))Error in .$start:.$end : NA/NaN argument
  • 可能是因为对于某些观察,“sanction_period”是例如 1990 年持续的,因此当我将列分开并将 end(year) 转换为数字时,我会得到有结尾的观察的 NA持续的一年。因此,某些观察没有结束年份,并且 R 需要有才能运行以下命令?
  • 是的,没错。我已经修改了示例数据和解决方案,以说明结束制裁期间年份为“正在进行”的行。
  • 太棒了!有机会时请把答案标记为解决方案。
【解决方案2】:

使用dplyr

left_join(GNIPC, sanctions, by=c("Country"="country", "Year"="Year")) %>%
  select(Country,Year, GNIpc, Imposition, sanctiontype)

【讨论】:

  • 谢谢。但是在第二个数据框中,我没有年份变量,而是范围制裁_周期
  • 正如joran 在评论中指出的那样,您需要整理数据。即:阿富汗 1 1 6 8 1997-2001
  • 抱歉:正如joran 在评论中指出的那样,您需要整理数据。即:Afghanistan 1 1 6 8 1997-2001需要变成15行,范围内sanctiontypeyear各一行。
猜你喜欢
  • 2020-07-17
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2020-05-29
  • 1970-01-01
相关资源
最近更新 更多