Hmm...I don't think I fully understand.
Are you trying to group by company and year, and add a column to represent the sum of sales over that year (without dropping the original columns)?
In which case, does this reprex show an example?
suppressPackageStartupMessages(library(tidyverse))
df1 <- tibble::tribble(
~company, ~year, ~quarter, ~quarterly_sales,
"A", 2017, 1, 565.86,
"A", 2017, 2, 566.18,
"A", 2017, 3, 208.97,
"A", 2017, 4, 988.82,
"A", 2018, 1, 569.87,
"A", 2018, 2, 446.68,
"A", 2018, 3, 32.95,
"A", 2018, 4, 979.55,
"B", 2017, 1, 309.02,
"B", 2017, 2, 716.19,
"B", 2017, 3, 425.87,
"B", 2017, 4, 597.01,
"B", 2018, 1, 455.4,
"B", 2018, 2, 737.73,
"B", 2018, 3, 785.51,
"B", 2018, 4, 259.34
)
df1
#> # A tibble: 16 x 4
#> company year quarter quarterly_sales
#> <chr> <dbl> <dbl> <dbl>
#> 1 A 2017 1 566.
#> 2 A 2017 2 566.
#> 3 A 2017 3 209.
#> 4 A 2017 4 989.
#> 5 A 2018 1 570.
#> 6 A 2018 2 447.
#> 7 A 2018 3 33.0
#> 8 A 2018 4 980.
#> 9 B 2017 1 309.
#> 10 B 2017 2 716.
#> 11 B 2017 3 426.
#> 12 B 2017 4 597.
#> 13 B 2018 1 455.
#> 14 B 2018 2 738.
#> 15 B 2018 3 786.
#> 16 B 2018 4 259.
df1 %>%
group_by(company, year) %>%
mutate(annual_sales_per_company = sum(quarterly_sales))
#> # A tibble: 16 x 5
#> # Groups: company, year [4]
#> company year quarter quarterly_sales annual_sales_per_company
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 A 2017 1 566. 2330.
#> 2 A 2017 2 566. 2330.
#> 3 A 2017 3 209. 2330.
#> 4 A 2017 4 989. 2330.
#> 5 A 2018 1 570. 2029.
#> 6 A 2018 2 447. 2029.
#> 7 A 2018 3 33.0 2029.
#> 8 A 2018 4 980. 2029.
#> 9 B 2017 1 309. 2048.
#> 10 B 2017 2 716. 2048.
#> 11 B 2017 3 426. 2048.
#> 12 B 2017 4 597. 2048.
#> 13 B 2018 1 455. 2238.
#> 14 B 2018 2 738. 2238.
#> 15 B 2018 3 786. 2238.
#> 16 B 2018 4 259. 2238.
Created on 2019-08-13 by the reprex package (v0.3.0)