【问题标题】:Aggregating percentage for one column by categories in another column in R在R中的另一列中按类别汇总一列的百分比
【发布时间】:2018-05-01 22:01:53
【问题描述】:

我知道这是基本的,但我遇到了问题。我从以下位置获取样本数据:

Link to article containing sample data

companiesData <- data.frame(fy = c(2010,2011,2012,2010,2011,2012,2010,2011,2012),
                            company = c("Apple","Apple","Apple","Google","Google","Google",
                                        "Microsoft","Microsoft","Microsoft"),
                            revenue = c(65225,108249,156508,29321,37905,50175,
                                        62484,69943,73723), 
                            profit = c(14013,25922,41733,8505,9737,10737,
                                       18760,23150,16978))

如何计算每家公司每年的利润百分比?例如,将 Apple 的所有利润相加,然后酌情将每个苹果行的总和百分比相加。最终结果应该是一个包含所有列但仅按公司使用百分比利润汇总的表格。岁月依旧。 苹果第一行的答案是 17.16%,计算公式为:

(14013/81668)*100

其中 81668 是苹果的总数,17.16% 是苹果第一行的利润百分比,即 2010 年。我不希望将其作为时间序列来完成,因为变量可能不一定是时间。可能是位置。

【问题讨论】:

  • edit您的问题包括您的示例数据的预期输出。
  • 嗨。我在问题中添加了一个结果示例。提前致谢。

标签: r dplyr aggregate plyr


【解决方案1】:

使用基础 r:

fun=function(x)paste0(round(x/sum(x)*100,2),"%")
transform(companiesData,prec=ave(profit,company,FUN=fun))
    fy   company revenue profit   prec
1 2010     Apple   65225  14013 17.16%
2 2011     Apple  108249  25922 31.74%
3 2012     Apple  156508  41733  51.1%
4 2010    Google   29321   8505 29.35%
5 2011    Google   37905   9737  33.6%
6 2012    Google   50175  10737 37.05%
7 2010 Microsoft   62484  18760 31.86%
8 2011 Microsoft   69943  23150 39.31%
9 2012 Microsoft   73723  16978 28.83%


library(data.table)
setDT(companiesData)[,prec:=profit/sum(profit)*100,by=company][]
     fy   company revenue profit     prec
1: 2010     Apple   65225  14013 17.15850
2: 2011     Apple  108249  25922 31.74071
3: 2012     Apple  156508  41733 51.10080
4: 2010    Google   29321   8505 29.34884
5: 2011    Google   37905   9737 33.60019
6: 2012    Google   50175  10737 37.05097
7: 2010 Microsoft   62484  18760 31.85708
8: 2011 Microsoft   69943  23150 39.31191
9: 2012 Microsoft   73723  16978 28.83100

【讨论】:

    【解决方案2】:

    dplyr 解决方案:按公司分组,将该公司的所有利润相加,然后创建一个新变量,即每年的利润占总利润的份额。

    library(dplyr)
    
    # delete reading in data from OP
    
    companiesData %>%
        group_by(company) %>%
        mutate(total_profit = sum(profit)) %>%
        mutate(share_this_yr = profit / total_profit)
    #> # A tibble: 9 x 6
    #> # Groups:   company [3]
    #>      fy company   revenue profit total_profit share_this_yr
    #>   <dbl> <fct>       <dbl>  <dbl>        <dbl>         <dbl>
    #> 1  2010 Apple       65225  14013        81668         0.172
    #> 2  2011 Apple      108249  25922        81668         0.317
    #> 3  2012 Apple      156508  41733        81668         0.511
    #> 4  2010 Google      29321   8505        28979         0.293
    #> 5  2011 Google      37905   9737        28979         0.336
    #> 6  2012 Google      50175  10737        28979         0.371
    #> 7  2010 Microsoft   62484  18760        58888         0.319
    #> 8  2011 Microsoft   69943  23150        58888         0.393
    #> 9  2012 Microsoft   73723  16978        58888         0.288
    

    reprex package (v0.2.0) 于 2018 年 5 月 1 日创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2022-01-23
      • 2021-09-28
      • 2021-10-15
      • 2019-03-02
      相关资源
      最近更新 更多