2016-08-23 4 views
3

私のデータは、制裁年度ごとに制裁対象を定めています。対象とする制裁には5つのタイプがありますが、具体的な制裁を見るのに興味があるので、対象となる年に制定された制裁対象を列挙して新しい列を作成したいと思います。4つの変数から1つの列を作成

df1 <- data.frame(Country = 'Angola', 
       Asset_freeze = c(1, 0), 
       Sectoral = c(1, 0), 
       Commodity = c(1, 0), 
       Diplomatic = c(1, 0), 
       Individual = c(1, 0), 
       Year = c('1993', '1994', '1995') 

    Country Asset_freeze Sectoral Commodity Diplomatic Individual Year 
    (chr)  (dbl) (dbl)  (dbl)  (dbl)  (dbl) (int) 
1 Angola    0  1   1   0   0 1993 
2 Angola    0  1   1   0   0 1994 
3 Angola    0  1   1   0   0 1995 

私はそれは次のようになりたいと思います:

 Country   Year Sanctions 
    (chr)    (int)  (dbl) 
1 Angola    1993  1 
2 Angola    1994  1 
3 Angola    1995  1 

どのように私はこれを得ることができますか?おかげ

+0

この場合、「Sanctions」は0ですか? –

+1

入力データセットコードでエラーが発生しています – akrun

+0

問題を具体的な問題ではなくコーディングの問題に焦点を当てて質問してください。制裁の意味を解釈する必要はありません。各年の中央の5つの列に0以外の項目があるかどうかを調べますか? @akrun:私はそれが最後に行方不明であると思います。 – Bazz

答えて

3

あなたは合計(rowSumsSanctionsの5種類を含む列を行方向と制裁のいずれかが課されているかどうかを確認して、あなたはまた、使用することができas.numeric

cbind(df1[c("Country", "Year")], Sanctions = as.numeric(rowSums(df1[, 2:6]) > 0)) 


# Country Year Sanctions 
#1 Angola 1993   1 
#2 Angola 1994   1 
#3 Angola 1995   1 
+1

これはうまくいきました、ありがとう! – MB92

2

を使用して数値するブール値に変換することができますcbindapplyifelseの組み合わせ:

cbind(df1[,c(1,7)], Sanctions=apply(df1[,2:6], 1, function(x) { 
    ifelse(any(x==1), 1, 0) 
})) 

Country Year Sanctions 
Angola 1993 1   
Angola 1994 1   
Angola 1995 1 

@Bazzによって示唆されるように、これは以下のようにして短縮されている可能性が:

cbind(df1[,c(1,7)], Sanctions=as.numeric(apply(df1[,2:6], 1, any))) 

ここで、列は名前ではなくインデックス番号で選択されます。しかし、あなたが望むなら、あなたは簡単に名前で列をつかむことができます。

こちらがお役に立てば幸いです。

+1

あなたは単に 'apply(df1 [、2:6]、1、any)'を持つことができます。出力は整数ではなく論理値になります。 – Bazz

+0

私は 'warnings'を生成する解決策を提供することを避けようとしていました。また、何が起こっているのかを明示するだけです。しかし、私は答えを編集することを確認します。 – Abdou

2

あなたはdplyrを使用することができ、そして得られたコマンドは、あなたが達成したいものを伝える:私たちは列2のためのpmax使用することができ

library(dplyr) 
df1 %>% group_by(Country, Year) %>% 
     mutate(Sanctions = as.numeric(any(Asset_freeze, Sectoral, Commodity, Diplomatic, Individual))) %>% 
     select(Country, Year, Sanctions) 
## Country Year Sanctions 
## <fctr> <fctr>  <dbl> 
##1 Angola 1993   1 
##2 Angola 1994   1 
##3 Angola 1995   1 
2

:6を、それが自動的に最大値を拾う必要があり

cbind(df1[c("Country", "Year")], Sanctions = do.call(pmax, df1[2:6])) 
# Country Year Sanctions 
#1 Angola 1993   1 
#2 Angola 1994   1 
#3 Angola 1995   1 
2

使用data.table

require(data.table) 

setDT(df1) 

NSanc <- 5L 

df1[, list(Sanctions = do.call(any, .SD)), 
    by = c("Country", "Year"), 
    .SDcols = 2:(NSanc + 1)] 

NSA ncは制裁の種類の数です。

関連する問題