Growth Increase, Groupby over time in R

This is one way to do it

library(dplyr)

sample_df <- data.frame(
  stringsAsFactors = FALSE,
              date = c("1/1/2020","1/1/2020",
                       "1/1/2020","1/1/2020","1/1/2020","1/1/2020","2/1/2020",
                       "2/1/2020","2/1/2020","2/1/2020","2/1/2020","2/1/2020"),
              size = c(1, 1, 3, 1, 2, 0, 5, 6, 3, 20, 21, 30),
              type = c("a","a","a","b","b","b",
                       "a","a","a","b","b","b")
)

sample_df %>% 
    group_by(date, type) %>% 
    summarise(diff = last(size) - first(size),
              increase = scales::percent(diff / first(size))) %>% 
    arrange(type, date)
#> `summarise()` regrouping output by 'date' (override with `.groups` argument)
#> # A tibble: 4 x 4
#> # Groups:   date [2]
#>   date     type   diff increase
#>   <chr>    <chr> <dbl> <chr>   
#> 1 1/1/2020 a         2 200%    
#> 2 2/1/2020 a        -2 -40%    
#> 3 1/1/2020 b        -1 -100%   
#> 4 2/1/2020 b        10 50%

Created on 2020-11-03 by the reprex package (v0.3.0.9001)

Note: Next time, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like