每年的百分比结构

Percentage structure for each year

我正在尝试使用一个函数来估计百分比结构,然后将其绘制在图上。下面你可以看到我的代码和数据

df<-data.frame(
  Years = rep(2010:2020,len=22),
  Stores = rep(c("Store1","Store2","Store3"),22),
  Revenues = rep(c(200,400,100),22))
    
df$Years<-as.factor(df$Years)
df$color <- factor(df$Revenues, labels = c("cyan", "green","darkviolet")) number of factor
df$Pct<-prop.table(df$Revenues) # <--This don't estimate percentage structure by year properly

此代码的最后一行没有按年份正确估计百分比结构,或者换句话说,估计整个列没有按年份分开。

那么谁能帮我解决这个问题并分别估算每年的百分比结构?

如果我理解正确,你可以这样做:

df$color <- factor(df$Revenues, labels = c("cyan", "green","darkviolet")) 
library(dplyr)
group_by(df, Years) %>% mutate(percent = Revenues/sum(Revenues))

输出:

# A tibble: 66 × 5
# Groups:   Years [11]
   Years Stores Revenues color      percent
   <int> <chr>     <dbl> <fct>        <dbl>
 1  2010 Store1      200 green       0.143 
 2  2011 Store2      400 darkviolet  0.286 
 3  2012 Store3      100 cyan        0.0714
 4  2013 Store1      200 green       0.143 
 5  2014 Store2      400 darkviolet  0.286 
 6  2015 Store3      100 cyan        0.0714
 7  2016 Store1      200 green       0.143 
 8  2017 Store2      400 darkviolet  0.286 
 9  2018 Store3      100 cyan        0.0714
10  2019 Store1      200 green       0.143 
# … with 56 more rows

base R中,我们可以使用ave

df$Pct <- with(df, ave(Revenues, Years, FUN = proportions))

-输出

> head(df)
  Years Stores Revenues      color        Pct
1  2010 Store1      200      green 0.14285714
2  2011 Store2      400 darkviolet 0.28571429
3  2012 Store3      100       cyan 0.07142857
4  2013 Store1      200      green 0.14285714
5  2014 Store2      400 darkviolet 0.28571429
6  2015 Store3      100       cyan 0.07142857