dplyr并计算每年购买人数的比例

时间:2018-10-11 19:45:55

标签: r dplyr

我有类似以下数据;

我正在尝试计算每年购买特定品牌的人所占的百分比。即40%的人每年购买品牌ID 9938,2%的人每年购买品牌1283等。

我正在尝试dplyr-group_bymutate,但似乎无法计算购买每个品牌的人的比例。

PANID是个人,因此此人在2001年购买了3个该品牌的商品。

    PANID year brandID
1 1100016 2001     115
2 1100016 2001     115
3 1100016 2001     115

任何帮助都会很棒。

df <- structure(list(PANID = c(1100016L, 1100016L, 1100016L, 1100016L, 
    1100016L, 1100016L, 1100016L, 1100016L, 1100016L, 1100016L, 1100016L, 
    1100032L, 1100057L, 1100057L, 1100057L, 1100057L, 1100057L, 1100057L, 
    1100057L, 1100057L, 1100057L, 1100180L, 1100180L, 1100180L, 1100180L, 
    1100180L, 1100180L, 1100180L, 1100180L, 1100214L, 1100248L, 1100248L, 
    1100321L, 1100321L, 1100321L, 1100321L, 1100321L, 1100404L, 1100404L, 
    1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 
    1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 1100404L, 
    1100404L, 1100420L, 1100479L, 1100503L, 1100503L, 1100545L, 1100545L, 
    1100545L, 1100545L, 1100560L, 1100560L, 1100560L, 1100560L, 1100560L, 
    1100560L, 1100560L, 1100560L, 1100560L, 1100560L, 1100560L, 1100560L, 
    1100560L, 1100560L, 1100685L, 1100685L, 1100685L, 1100685L, 1100685L, 
    1100685L, 1100685L, 1100685L, 1100685L, 1100685L, 1100685L, 1100750L, 
    1100750L, 1100750L, 1100750L, 1100784L, 1101162L, 1101162L, 1101162L, 
    1101162L, 1101162L, 1101162L, 1101238L, 1101253L, 1101253L, 1101253L, 
    1101253L, 1101253L, 1101253L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 
    1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101279L, 1101642L, 
    1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 
    1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 1101642L, 
    1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 
    1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 
    1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 
    1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 1101758L, 
    1101758L, 1101758L, 1102012L, 1102012L, 1102046L, 1102046L, 1102202L, 
    1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 
    1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 
    1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 
    1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 
    1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 1102202L, 
    1102277L, 1102277L, 1102277L, 1102368L, 1102368L, 1102384L, 1102384L, 
    1102384L, 1102491L, 1102491L, 1102491L, 1102491L, 1102491L, 1102491L, 
    1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 
    1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 1102772L, 
    1103069L, 1103069L, 1103069L, 1103069L, 1103069L, 1103101L, 1103101L, 
    1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 
    1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 
    1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 1103101L, 
    1103101L, 1103325L, 1103325L, 1103325L, 1103325L, 1103440L, 1103440L, 
    1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 
    1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 1103614L, 
    1103671L, 1103671L, 1103671L, 1103895L, 1103895L, 1103895L, 1103895L, 
    1103895L, 1103895L, 1103895L, 1103895L, 1103895L, 1103895L, 1103895L, 
    1103895L, 1103895L, 1103895L, 1103895L, 1103911L, 1103911L, 1104042L, 
    1104042L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 
    1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 
    1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 1104182L, 
    1104182L, 1104182L, 1104182L, 1104208L, 1104208L, 1104208L, 1104208L, 
    1104224L, 1104257L, 1104257L, 1104257L, 1104257L, 1104273L, 1104273L, 
    1104315L, 1104315L, 1104315L, 1104315L, 1104364L, 1104372L, 1104372L, 
    1104372L, 1104372L, 1104406L, 1104406L, 1104406L, 1104406L, 1104414L, 
    1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 
    1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 
    1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 
    1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 1104414L, 
    1104612L, 1104703L, 1104703L, 1104703L, 1104869L, 1104869L, 1104869L, 
    1104935L, 1104968L, 1104968L, 1104968L, 1104968L, 1104968L, 1104968L, 
    1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 
    1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 1105171L, 
    1105171L, 1105171L, 1105213L, 1105213L, 1105213L, 1105239L, 1105239L, 
    1105239L, 1105239L, 1105239L, 1105254L, 1105254L, 1105254L, 1105726L, 
    1105726L, 1105759L, 1105759L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 1105775L, 
    1105809L, 1105965L, 1105965L, 1105965L, 1105965L, 1105965L, 1105965L, 
    1105965L, 1105965L, 1105965L, 1105965L, 1105965L, 1105965L, 1105965L, 
    1105965L, 1105965L, 1105965L, 1105965L, 1105999L, 1105999L, 1105999L, 
    1105999L, 1105999L, 1106146L, 1106146L, 1106146L, 1106229L, 1106229L, 
    1106294L, 1106484L, 1106567L, 1106567L, 1106567L, 1106567L, 1106567L, 
    1106567L, 1106567L, 1106567L, 1106567L, 1106567L, 1106567L, 1106575L, 
    1106575L, 1106575L, 1106575L, 1106575L, 1106575L, 1106575L, 1106575L, 
    1106575L, 1106575L, 1106575L, 1106575L, 1106864L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 1107235L, 
    1107250L, 1107250L, 1107318L, 1107391L, 1107391L, 1107425L, 1107425L, 
    1107425L, 1107425L, 1107516L, 1107516L, 1107516L, 1107516L, 1107516L, 
    1107516L, 1107516L, 1107581L, 1107581L, 1107581L, 1107581L, 1107581L, 
    1107581L, 1107581L, 1107581L, 1107847L, 1107847L, 1107847L, 1107847L, 
    1107847L, 1107847L, 1107847L, 1107847L, 1107847L, 1107847L, 1107847L, 
    1107847L, 1107847L, 1107847L, 1107847L, 1107847L, 1107862L, 1107862L, 
    1107862L, 1107920L, 1107920L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 1107953L, 
    1107953L, 1107953L, 1107961L, 1107961L, 1107961L, 1107961L, 1107961L, 
    1107961L, 1107961L, 1107961L, 1108043L, 1108043L, 1108043L, 1108043L, 
    1108043L, 1108043L, 1108043L, 1108225L, 1108225L, 1108266L, 1108308L, 
    1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 
    1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 
    1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 1108308L, 
    1108308L, 1108308L, 1108308L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 1108456L, 
    1108456L, 1108456L, 1108456L, 1108464L, 1108464L, 1108464L, 1108464L, 
    1108464L, 1108464L, 1108464L, 1108464L, 1108464L, 1108464L, 1108464L, 
    1108464L, 1108464L, 1108464L, 1108464L, 1108464L, 1108670L, 1108670L, 
    1108670L, 1108670L, 1108670L, 1108670L, 1108670L, 1108795L, 1108795L, 
    1108878L, 1109165L, 1109165L, 1109165L, 1109165L, 1109165L, 1109165L, 
    1109272L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
    1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
    1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
    1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
    1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 1109306L, 
    1109447L, 1109447L, 1109447L, 1109447L, 1109447L, 1109447L, 1109447L, 
    1109447L, 1109447L), year = c(2001, 2001, 2001, 2002, 2002, 2002, 
    2002, 2003, 2004, 2004, 2005, 2004, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2007, 2001, 2001, 2002, 2005, 2005, 2005, 2005, 
    2007, 2004, 2005, 2007, 2001, 2002, 2002, 2004, 2005, 2001, 2001, 
    2003, 2004, 2006, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 
    2007, 2007, 2007, 2007, 2002, 2004, 2003, 2007, 2001, 2003, 2004, 
    2005, 2001, 2001, 2001, 2002, 2002, 2003, 2004, 2004, 2005, 2005, 
    2005, 2005, 2006, 2006, 2002, 2002, 2002, 2002, 2002, 2004, 2004, 
    2004, 2005, 2005, 2005, 2003, 2003, 2006, 2007, 2005, 2001, 2001, 
    2002, 2003, 2004, 2004, 2001, 2004, 2006, 2006, 2007, 2007, 2007, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 2002, 
    2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
    2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
    2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2005, 2005, 2005, 
    2005, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2003, 2003, 
    2003, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 2006, 2006, 
    2007, 2007, 2001, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2006, 2006, 2006, 2007, 2007, 2007, 2001, 
    2003, 2001, 2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 
    2004, 2004, 2004, 2004, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
    2006, 2007, 2007, 2007, 2007, 2007, 2005, 2005, 2005, 2006, 2007, 
    2001, 2001, 2003, 2002, 2003, 2003, 2004, 2006, 2007, 2003, 2003, 
    2003, 2003, 2003, 2004, 2004, 2005, 2005, 2005, 2006, 2007, 2007, 
    2007, 2001, 2002, 2003, 2003, 2004, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 2003, 2004, 
    2005, 2005, 2005, 2006, 2006, 2006, 2006, 2007, 2001, 2002, 2002, 
    2002, 2005, 2007, 2001, 2002, 2002, 2002, 2002, 2002, 2002, 2003, 
    2003, 2003, 2003, 2004, 2004, 2006, 2001, 2001, 2002, 2001, 2001, 
    2001, 2002, 2002, 2003, 2003, 2004, 2004, 2005, 2005, 2005, 2006, 
    2007, 2007, 2002, 2002, 2004, 2006, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 
    2003, 2003, 2004, 2004, 2004, 2004, 2006, 2001, 2001, 2002, 2003, 
    2001, 2005, 2005, 2005, 2005, 2001, 2002, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2002, 2002, 2001, 2003, 2004, 2005, 2002, 2002, 
    2002, 2003, 2003, 2003, 2003, 2004, 2005, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
    2006, 2007, 2007, 2007, 2007, 2005, 2004, 2004, 2004, 2001, 2006, 
    2007, 2004, 2001, 2003, 2005, 2007, 2007, 2007, 2001, 2001, 2003, 
    2003, 2003, 2004, 2004, 2004, 2005, 2005, 2006, 2006, 2006, 2007, 
    2007, 2007, 2003, 2006, 2006, 2001, 2001, 2001, 2002, 2003, 2003, 
    2004, 2004, 2003, 2007, 2002, 2006, 2002, 2002, 2002, 2002, 2002, 
    2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 
    2005, 2005, 2005, 2005, 2004, 2001, 2003, 2003, 2003, 2003, 2004, 
    2004, 2005, 2005, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007, 
    2001, 2002, 2003, 2004, 2007, 2001, 2002, 2002, 2005, 2005, 2003, 
    2007, 2001, 2001, 2001, 2002, 2002, 2003, 2004, 2004, 2004, 2006, 
    2006, 2001, 2002, 2002, 2003, 2003, 2003, 2003, 2005, 2005, 2007, 
    2007, 2007, 2004, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2002, 
    2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
    2004, 2004, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 
    2006, 2007, 2007, 2003, 2006, 2002, 2005, 2005, 2001, 2001, 2002, 
    2002, 2004, 2005, 2005, 2006, 2006, 2006, 2006, 2001, 2001, 2002, 
    2003, 2003, 2003, 2003, 2006, 2001, 2001, 2002, 2003, 2003, 2003, 
    2003, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2001, 
    2004, 2007, 2004, 2004, 2001, 2001, 2002, 2002, 2002, 2002, 2002, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
    2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 
    2006, 2006, 2006, 2007, 2007, 2007, 2007, 2007, 2004, 2004, 2004, 
    2004, 2005, 2005, 2005, 2006, 2001, 2002, 2002, 2002, 2003, 2003, 
    2005, 2001, 2006, 2003, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
    2002, 2003, 2004, 2004, 2004, 2004, 2005, 2005, 2006, 2006, 2006, 
    2006, 2007, 2007, 2007, 2007, 2007, 2007, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
    2001, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
    2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
    2002, 2002, 2002, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004, 2004, 
    2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 2005, 
    2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2006, 2006, 
    2006, 2006, 2006, 2006, 2006, 2002, 2002, 2002, 2002, 2003, 2003, 
    2003, 2003, 2003, 2003, 2003, 2004, 2004, 2004, 2004, 2007, 2002, 
    2002, 2002, 2003, 2003, 2005, 2005, 2005, 2007, 2007, 2001, 2001, 
    2002, 2002, 2003, 2003, 2005, 2001, 2001, 2001, 2001, 2001, 2001, 
    2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2002, 
    2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2003, 2003, 2003, 
    2003, 2004, 2004, 2004, 2004, 2006, 2001, 2001, 2001, 2002, 2002, 
    2002, 2002, 2002, 2002), brandID = c(115, 115, 115, 115, 115, 
    115, 115, 491, 73, 73, 73, 311, 433, 430, 448, 73, 73, 196, 196, 
    196, 432, 283, 445, 313, 315, 315, 115, 315, 315, 113, 480, 535, 
    72, 73, 277, 315, 288, 117, 432, 288, 491, 303, 134, 305, 305, 
    115, 305, 80, 175, 162, 163, 300, 46, 45, 311, 313, 73, 342, 
    311, 311, 311, 311, 447, 430, 445, 20, 73, 73, 445, 446, 73, 
    446, 72, 73, 433, 446, 480, 73, 283, 180, 480, 72, 73, 73, 180, 
    72, 288, 72, 72, 73, 73, 73, 115, 115, 115, 115, 115, 115, 72, 
    491, 277, 277, 369, 277, 277, 73, 72, 73, 72, 315, 73, 72, 315, 
    73, 72, 315, 73, 72, 73, 72, 73, 72, 72, 73, 72, 315, 73, 72, 
    72, 315, 80, 72, 73, 72, 315, 73, 72, 72, 315, 72, 72, 73, 72, 
    315, 73, 72, 73, 72, 73, 72, 315, 73, 72, 73, 72, 315, 73, 73, 
    72, 315, 315, 73, 72, 73, 72, 73, 72, 315, 73, 72, 315, 73, 72, 
    73, 73, 72, 72, 73, 72, 315, 73, 72, 73, 72, 73, 72, 315, 73, 
    72, 73, 72, 315, 73, 73, 72, 73, 72, 73, 73, 72, 72, 72, 72, 
    72, 72, 72, 72, 72, 72, 534, 72, 283, 72, 72, 72, 325, 283, 288, 
    288, 288, 288, 72, 288, 288, 288, 288, 288, 288, 283, 339, 72, 
    72, 363, 72, 433, 288, 288, 288, 288, 326, 288, 288, 283, 434, 
    288, 283, 443, 443, 423, 443, 72, 443, 72, 443, 288, 175, 288, 
    288, 315, 315, 73, 480, 180, 14, 515, 175, 113, 425, 175, 134, 
    173, 445, 423, 308, 115, 423, 308, 115, 534, 534, 113, 534, 534, 
    115, 534, 534, 100, 490, 118, 115, 100, 100, 149, 115, 425, 310, 
    115, 115, 72, 433, 360, 117, 73, 433, 444, 446, 313, 115, 73, 
    72, 72, 72, 73, 79, 446, 73, 73, 79, 73, 73, 491, 73, 73, 446, 
    73, 73, 73, 315, 425, 315, 425, 73, 73, 73, 333, 333, 333, 333, 
    333, 73, 73, 73, 332, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 
    73, 73, 73, 162, 446, 173, 180, 514, 124, 72, 72, 72, 221, 118, 
    342, 72, 72, 72, 433, 17, 480, 443, 72, 80, 72, 303, 303, 433, 
    433, 548, 20, 433, 446, 432, 446, 446, 446, 446, 446, 446, 72, 
    72, 222, 73, 438, 278, 463, 438, 438, 142, 438, 339, 142, 438, 
    438, 142, 438, 142, 438, 438, 438, 142, 438, 438, 438, 463, 339, 
    448, 448, 480, 115, 79, 72, 72, 72, 72, 117, 73, 433, 436, 438, 
    339, 444, 115, 115, 115, 115, 115, 115, 277, 115, 14, 72, 115, 
    72, 118, 426, 200, 72, 73, 5, 443, 425, 491, 364, 118, 117, 434, 
    434, 463, 72, 269, 443, 443, 443, 463, 443, 443, 443, 443, 462, 
    115, 115, 115, 117, 443, 176, 180, 369, 80, 434, 113, 433, 175, 
    313, 315, 73, 73, 73, 73, 73, 115, 73, 315, 73, 315, 79, 277, 
    115, 73, 113, 277, 311, 430, 448, 444, 149, 149, 180, 221, 324, 
    288, 486, 480, 73, 149, 149, 443, 283, 303, 149, 116, 116, 145, 
    145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 
    145, 115, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 283, 
    145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 433, 145, 
    145, 145, 145, 145, 277, 277, 277, 311, 180, 73, 180, 73, 73, 
    73, 73, 73, 73, 433, 180, 180, 73, 180, 446, 162, 162, 115, 115, 
    115, 73, 72, 433, 115, 303, 115, 315, 221, 221, 444, 221, 72, 
    221, 315, 221, 142, 433, 115, 115, 99, 283, 325, 325, 491, 72, 
    491, 491, 16, 433, 60, 72, 118, 117, 118, 283, 118, 118, 118, 
    327, 117, 222, 117, 118, 117, 466, 277, 450, 175, 443, 288, 149, 
    5, 117, 118, 443, 124, 134, 117, 472, 472, 173, 73, 149, 413, 
    22, 492, 118, 360, 149, 386, 443, 427, 386, 413, 149, 180, 277, 
    425, 342, 342, 480, 529, 149, 180, 115, 315, 222, 100, 315, 222, 
    222, 79, 79, 79, 79, 73, 79, 79, 342, 117, 117, 117, 491, 73, 
    491, 288, 491, 491, 491, 73, 73, 73, 14, 490, 18, 72, 73, 73, 
    313, 313, 221, 221, 222, 221, 221, 480, 221, 221, 221, 221, 142, 
    142, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 
    221, 142, 142, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 
    221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 73, 73, 
    73, 73, 73, 73, 73, 73, 283, 480, 72, 433, 72, 72, 72, 303, 369, 
    224, 180, 1, 20, 162, 180, 22, 1, 1, 433, 1, 1, 20, 1, 1, 1, 
    180, 1, 1, 315, 433, 23, 492, 23, 492, 20, 115, 115, 175, 115, 
    115, 73, 73, 73, 115, 175, 115, 73, 369, 73, 73, 73, 73, 73, 
    73, 115, 115, 115, 115, 115, 115, 303, 115, 303, 115, 115, 115, 
    115, 115, 115, 115, 73, 73, 115, 303, 115, 73, 73, 115, 73, 115, 
    303, 303, 14, 73, 115, 303, 73, 115, 303, 73, 303, 73, 303, 73, 
    73, 115, 303, 73, 115, 115, 303, 73, 115, 303, 73, 115, 73, 73, 
    115, 303, 73, 73, 115, 303, 115, 73, 73, 73, 288, 115, 303, 303, 
    73, 115, 303, 491, 73, 303, 73, 73, 303, 73, 73, 18, 73, 73, 
    115, 534, 73, 534, 73, 73, 73, 534, 73, 73, 534, 73, 73, 73, 
    534, 73, 534, 491, 343, 145, 145, 343, 145, 145, 208, 145, 145, 
    69, 145, 145, 145, 343, 487, 69, 72, 73, 72, 73, 72, 72, 72, 
    438, 433, 115, 369, 216, 216, 316, 72, 432, 369, 315, 315, 315, 
    315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 
    315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 
    315, 315, 315, 315, 73, 548, 72, 283, 72, 72, 72, 117, 72, 72
    )), row.names = c(NA, 1000L), class = "data.frame")

2 个答案:

答案 0 :(得分:3)

这是方便的dplyr函数n_distinct()的好用例,该函数计算一列中唯一值的数量。

library(dplyr)
library(tidyr)

df <- data_frame(Year = c(2001, 2001, 2002, 2002, 2003, 2003, 2003),
                 Customer = c('Sam', 'Arjun', 'Sam', 'Sam', 'Arjun', 'Fatima', 'Leah'),
                 Brand = c('A', 'A', 'A', 'B', 'A', 'A', 'B'))

# For each year, get the total number of distinct customers
  total_customers_in_year <- df %>% 
    group_by(Year) %>% 
      summarize(Years_Customers = n_distinct(Customer))

# Within each year, get each brand's total number of distinct customers
  brand_customers_in_year <- df %>% 
    group_by(Year, Brand) %>% 
      summarize(Brand_Customers_in_Year = n_distinct(Customer)) %>% 
    ungroup %>% 
    complete(Year, Brand, fill = list(Brand_Customers_in_Year = 0L))

# Combine the year-specific summary and brand-year-specific summary
  brand_customers_in_year %>% 
    left_join(y = total_customers_in_year,
              by = 'Year') %>% 
    mutate(Brand_Share_in_Year = Brand_Customers_in_Year/Years_Customers)

答案 1 :(得分:2)

这显示了百分比(Y年购买B品牌的客户数量)/(Y年购买B个客户数量的数量)

library(data.table)
setDT(df)

brands <- df[, unique(brandID)]
df2 <- df[, .(brandID = list(brandID)), .(PANID, year)]

df2[, .(brand = brands, 
        pct   = sapply(brands, function(x) mean(sapply(brandID, `%in%`, x = x))))
    , by = year]

#      year brand  pct
#   1: 2001   115 0.12
#   2: 2001   491    0
#   3: 2001    73 0.16
#   4: 2001   311 0.02
#   5: 2001   433 0.08
#  ---                
# 724: 2006   208    0
# 725: 2006    69    0
# 726: 2006   487    0
# 727: 2006   216    0