在两个数据集中合并基于多于1列的数据集

问题描述:

我试图合并两个数据集,按年份和国家/地区。第一组数据(df = GNIPC)代表1980-2008年间每个国家的人均国民收入。在两个数据集中合并基于多于1列的数据集

  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年来不在sanctions_period值是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 
+0

我不会用那种格式的第二个数据集。如果有人向我提供这些数据,我会(1)畏缩,(2)开始工作,以便每个'sanctiontype'组合和每个'sanctions_period'组合中都有一行。所以'Afganistan'将有五行,其中'sanctiontype = 1',每个年份为1997 - 2001年。 – joran

+0

阿富汗1998年应该是什么样子?每个制裁周期(2)都是一行,还是一行“1 4 6 7 8”? – Chris

+0

我已经完成了一个不同的数据集,其中每个制裁类型都有自己的行。在这里,我正在寻找一种方法来确定每个GNI年份当年是否存在制裁。回顾过去的制裁类型,我该怎么做? – MB92

一些示例数据:

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 

从那里,它应该很容易与您的第一个数据帧合并。请注意,您的第一个数据框大写了国家和年份,而第二个数据框没有。

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

我在我的数据集上做了以下操作,但出现错误: 'df.new % 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))' '错误。$ start:。$ end:NA/NaN参数' – MB92

+0

难道是因为对于某些观察'sanction_period'是例如1990年 - 正在进行,因此当我分开列并将结束(年份)转换为数字时,我得到的NA的观测值有结束年份。因此,对于某些观测而言,没有结束的一年,那么为了运行以下命令,需要R吗? – MB92

+0

是的,这是正确的。我修改了示例数据和解决方案来说明结束sanctions_period年份“正在进行中”的行。 – jdobres

使用dplyr

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

谢谢。然而,在第二个数据框中,我没有年份变量,而是一个范围sanctions_period – MB92

+1

正如'joran'在评论中指出的那样,您需要整理数据。那就是: 阿富汗1 1 6 8 1997-2001 –

+0

对不起:正如'joran'在评论中指出的,你需要整理你的数据。即: “阿富汗1 1 6 8 1997-2001”需要变成15行,每个“制裁类型”和“年份”在范围内各一个。 –