Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to create a table that calculates percent change between values in two separate tables in R?

$
0
0

I have two data tables, each representing the monthly spread in one year and the same in the next.

I would like to calculate the % change in values based on my index ('Category Name') so each row value reperesnets the % change from the first year to the next.

Reference the first years table below:

`Category Name`   Oct   Nov   Dec   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep  Average `Percent Total`
   <fct>           <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>    <dbl>           <dbl>
 1 Salty Snacks    36340 43950 34246 26758 36151 51390 35299 43337 35251 32449 46309 33534 37918.             39.4 
 2 Candy           23003 23814 19271 14269 21902 30986 22054 27269 22651 20789 30090 21632 23144.             24.0 
 3 Nutrition Bar   10563 12182  9657  7029 10740 15392 10926 14955 12370 10914 16130 11945 11900.             12.4 
 4 Cookies         10751 11679  9051  6752  9416 14467 10993 14364 12207 11066 14862  9656 11272              11.7 
 5 Nuts/Trailmix    4018  4371  3512  2676  3935  5586  3820  5182  4555  3869  5672  3938  4261.              4.43
 6 Pastry           3531  4328  3666  2853  3906  5396  3521  4318  3571  3520  4830  4026  3956.              4.11
 7 Meat Sticks      2131  2669  1919  1575  2156  3021  2088  2630  2369  2043  2759  2091  2288.              2.38
 8 Other            1500  2191  2133  1650  2151  2696  1196  1169   760   751   954   747  1492.              1.55
 9 Crackers            1     0     0     0     0     0     0     0    13    39    86    23    13.5             0.01
10 Food                0     0     0     0     0     2     7     3    15    25    30     9     7.58            0.01

And here is the next years table, same layout/parameters:

`Category Name`   Oct   Nov   Dec   Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep Average `Percent Total`
   <fct>           <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>           <dbl>
 1 Salty Snacks    35562 38043 37697 33079 31942 35862 35646 43987 33551 34916 46172 35511 36831.            39.1 
 2 Candy           22722 21071 20496 17663 19470 21834 21924 26846 21046 22482 29402 22477 22286.            23.6 
 3 Nutrition Bar   12339 12975 12073 10668 11813 12999 12897 17062 12982 13506 17234 13033 13298.            14.1 
 4 Cookies         10042  9712  9489  7820  8472  9936  9756 11867  9307  9525 12123  9544  9799.            10.4 
 5 Pastry           5322  5243  5185  4518  4546  4868  4819  5792  4275  4484  5547  4486  4924.             5.23
 6 Nuts/Trailmix    4236  4279  4034  3656  3733  4364  4171  5514  4209  4486  5595  4409  4390.             4.66
 7 Meat Sticks      2067  2195  2024  2085  2068  2236  2342  2814  2337  2311  3028  2428  2328.             2.47
 8 Crackers           12     4     2     1     0    49   185   459   355   550   839   705   263.             0.28
 9 Other             433   177    89    38    28    17     9     4     2     0     1     1    66.6            0.07
10 Food                4     0     5     2     0     0    11    46    48    65   121    77    31.6            0.03

I am trying to create a new table that shows % change for each month for each Category (Salty snacks, Candy, etc.). My plan is take this and create a formattable from it. Thanks!!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>