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

Create columns in a dataframe as a sum of previous columns for a bar chart race visualization in R

$
0
0

I am trying to calculate tax payments, made by companies each year, and show the sum of totally paid taxes after each year, using previous years and summarizing them.

After doing this, I want to show bar chart race (but this is beyond my question, just a clarification what I want).

I did it in a very rough manual way, using mutate.

This solution is terrible, I acknowledge and I would like to learn a more elegant way (especially with dplyr). I did my research, but did not find how to do it, maybe asking a wrong question in my search.

company_payments_clean %>% mutate(Sum2007 = `2006` + `2007`,
                                      Sum2008 = `2006` + `2007` + `2008`, 
                                      Sum2009 = `2006` + `2007` + `2008` + `2009`,
                                      Sum2010 = `2006` + `2007` + `2008` + `2009` + `2010`,
                                      Sum2011 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011`,
                                      Sum2012 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012`,
                                      Sum2013 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012` + `2013`,
                                      Sum2014 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012` + `2013` + `2014`,
                                      Sum2015 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012` + `2013` + `2014` + `2015`,
                                      Sum2016 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012` + `2013` + `2014` + `2015` + `2016`,
                                      Sum2017 = `2006` + `2007` + `2008` + `2009` + `2010` + `2011` + `2012` + `2013` + `2014` + `2015` + `2016` + `2017`) %>%
                                      select(`Name of organisation`, Sum2007, Sum2008, Sum2009, Sum2010, Sum2011, Sum2012, Sum2013, Sum2014, Sum2015, Sum2016, Sum2017)

My data looks like this (this is an open data, which is available online)

structure(list(`Name of organisation` = c("?????? ??? ???", "??????? ?????????", 
"????????? ????", "????n Erch", "ADAE", "Adamas mauntin"), `2006` = c(0, 
0, 0, 0, 0, 0), `2007` = c(0, 0, 0, 0, 0, 0), `2008` = c(0, 0, 
0, 0, 0, 0), `2009` = c(0, 0, 63791000, 736145300, 0, 0), `2010` = c(42064000, 
0, 0, 511734500, 0, 188945000), `2011` = c(0, 0, 0, 841354900, 
0, 0), `2012` = c(0, 0, 0, 1603328000, 0, 0), `2013` = c(0, 0, 
417182450, 289145100, 0, 197865350), `2014` = c(0, 0, 214928280, 
102937500, 0, 0), `2015` = c(104671449, 0, 0, 0, 175549000, 0
), `2016` = c(141311733.57, 0, 0, 0, 0, 0), `2017` = c(150181688.63, 
124363263, 0, 0, 0, 0)), row.names = c(NA, -6L), groups = structure(list(
`Name of organisation` = c("?????? ??? ???", "??????? ?????????", 
"????????? ????", "????n Erch", "ADAE", "Adamas mauntin"), 
.rows = list(1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"), .drop = FALSE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Please advise!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



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