更新: 誰かが疑問に思っている場合は、両方の答えが機能します。どちらも、Excel で Sumif をエミュレートする場合に作成するのと同じように、集計テーブルを作成します。これはまさに私が探していたものです。お二方に改めて感謝申し上げます。
このようなデータ フレーム (df) がありますが、より多くの製品があります。
df$Yr
カットオフ日 >= 2012 年 3 月に基づいています
Product Classif Yr Revenue
a paid_yes TRUE 25
a paid_yes TRUE 20
a paid_yes TRUE 35
a paid_yes FALSE 20
a paid_yes FALSE 30
a paid_yes FALSE 30
a paid_partial TRUE 15
a paid_partial TRUE 15
a paid_partial FALSE 18
a leased TRUE 12
a leased TRUE 12
a leased FALSE 14
a Other TRUE 27
a Other FALSE 30
a Other TRUE 25
a Other FALSE 22
a Other TRUE 32
a Other FALSE 30
a Other TRUE 24
a Other FALSE 27
b paid_yes TRUE 45
b paid_yes FALSE 32
b paid_yes TRUE 35
b paid_yes FALSE 39
b paid_partial FALSE 42
b paid_partial FALSE 45
b paid_partial TRUE 47
b paid_partial FALSE 33
b paid_partial FALSE 28
b leased TRUE 48
b leased FALSE 46
b leased FALSE 45
b leased TRUE 37
b leased FALSE 33
b leased TRUE 46
b leased FALSE 44
b Other TRUE 49
b Other FALSE 45
b Other TRUE 43
b Other FALSE 39
製品別(a、b、cなど)のファセット散布図を作成しようとしています。y 軸を、x 軸を、各 内df$Classif
の合計のパーセンテージにしたい。言い換えれば、特定の年の製品の総収益の何パーセントを各分類が占めているのでしょうか?Revenue
Product
Yr
要約フレームを次のようにしたい...
Product Classif Yr perc.rev
a paid_yes TRUE .332
a paid_partial TRUE .123
a leased TRUE .099
a Other TRUE .446
各 perc.rev は、Product
、Classif
、およびYr
次のコードを使用して、集計データ セット/列を取得しようとしました。
df.perc <- ddply(df, .(Product, Classif, Yr), summarise,
perc.rev = sum(Revenue)/count(Classif))
結果のデータ フレームから、、 、による平均収益が得られます。私が必要としているのは、与えられたによって生み出された収益のパーセンテージです。Product
Classif
Yr
Classif
Classif
Product
Year
perc.rev 式、.variables
またはddply
. 私は Excel に慣れており、通常は 2 つの sumifs 式を使用しますが、ここで行う必要があることを R 関数で表現する方法がわかりません。