Home > Back-end >  Percentage structure for each year
Percentage structure for each year

Time:06-04

I am trying to use a function that will estimate percentage structure and after that will be plot on the plot. Below you can see my code and data

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

The last line of this code doesn't estimate percentage structure by year properly or in other words, estimates the whole column not separate by year.

So can anybody help me how to solve this problem and estimate the percentages structure separately for each year?

CodePudding user response:

If I understand your correctly, you can do this:

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

Output:

# 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

CodePudding user response:

In base R, we can use ave

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

-output

> 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
  • Related