'R: Dataframe summary into a new dataframe [duplicate]

I'm really new into R and I'm working on a solution that needs to handle dataframe and charts.

I have an original DF like the one below:

Product_ID  Sales_2016  Sales_2017  Import
1   1162.91 235.19  1
2   1191.11 944.87  1
3   1214.96 737.06  0
4   1336.07 986.15  0
5   1343.29 871.33  1
6   1208.78 1168.82 0
7   1205.49 900.39  0
8   1675.69 2255.11 0
9   1412.55 1021.05 1
10  1546.06 1893.82 0
11  1393.98 1629.42 0
12  1264.76 1014.74 0
13  1399    2163.64 0
14  1712.75 1845.59 0
15  1310.67 1400.31 1
16  1727.8  1574.98 0
17  1987.14 3359.82 0
18  1711.99 2065.15 0
19  1570.97 2444.15 0
20  1546.2  1912.32 0

I want to build a stack bar chart with this data where it should show the year (2020/2021) on the X axis and the sales sum on the Y axis. The bar would split imported or not into two groups and stack them like the image bellow:

enter image description here

TO achieve it, my solution was to create another DF manually with the sum values:

Import   TotalSales   Year
1        123456       2016
0        654321       2016
1        456789       2017
0        987654       2017

I created this DF totally manually. My question is, is there a faster and more reliable way to create this chart and this kind of dataframe view?

Regards, Marcus



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source