2016-08-17 6 views
0

私は2つのデータセットを年と国でマージしようとしています。最初のデータセット(df = GNIPC)は、1980年から2008年までの各国の1カ国あたりの国民総所得を表す。両方のデータセットで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 

第2のデータセット(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年度の私はどちらかの国に存在するかどうか制裁を持っているように、2つのデータセットをマージしたいと思います。 sanctions_periodにないGNI年度の値が0になるとしているもののためには、これは私がそれが見えるようにしたいものです。1.次のようになります。dplyrを使用して

  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

私はその形式の2番目のデータセットでは動作しません。誰かが私にそのデータを渡したら(1)うんざりし、(2)それを「sanction_period」の中に 'sanctiontype'と毎年の組み合わせごとに一つの行があるように変換して働かせます。だからAfganistanは 'sanctiontype = 1'という5行を持っていて、1997年から2001年のそれぞれに1つずつ存在する。 – joran

+0

アフガニスタン1998の見た目はどうですか?制裁期間(2)ごとに1行、または「1 4 6 7 8」で1行になるでしょうか? – Chris

+0

私は各sanctionstypeが独自の行を持っている別のデータセットを行っています。ここでは、GNI年ごとにその年に制裁があるかどうかを判断する方法を探しています。どのように私はそれを行うことができます制裁の過去を見て? – MB92

答えて

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 

そこから、それが簡単にあなたの最初のデータフレームをマージする必要があります。最初のデータフレームは国と年を使用し、2番目のデータフレームは使用しません。

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

私のデータセットで次のようにしましたが、エラーが発生しました: 'df.new < - 分離(sanctions4、sanctions_period、c( '開始'、 '終了')、削除= F)%>% mutate(開始= (country、。$ country、imposition =。$賦課、sanctiontype =、国民年金)。 $ end:NA/NaN引数 ' – MB92

+0

'sanction_period'がいくつかの観測のためである可能性がありますか?例1990-進行中です。したがって、列とトランケーションの終わり(年)を数値で区切ってみると、終了年がある観測についてはNAが得られます。したがって、いくつかの観測では終了年はなく、Rは次のコマンドを実行するためにそこに存在する必要がありますか? – MB92

+0

はい、そうです。私は、終了するsanctions_period年が "進行中"の行を考慮に入れて、サンプルデータとソリューションを変更しました。 – jdobres

0

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

ありがとうございます。しかし、2番目のデータフレームでは、私は年変数を持たず、代わりに範囲sanctions_period – MB92

+1

'joran'がコメント内で指摘したように、あなたのデータを整理する必要があります。それは: アフガニスタン1 1 6 8 1997-2001 –

+0

申し訳ありません:ジョランはコメントで指摘したように、あなたのデータをきちんと整理する必要があります。 'Afghanistan 1 1 6 8 1997-2001'は15行になる必要があります.1つは' sanctiontype'と 'year'です。 –

関連する問題