Example data:
Project 2016 2017 2018 2019
Proj1 42 36 400 250
Proj2 96 780 60 900
Proj3 180 230 0 0
I have a set of financial data from this year, that comes with previous years' financial data as well. I am trying to mutate the data so that I add the previous three years into a "Previous Funding" column.
The data has columns labeled with 2016, 2017, 2018, 2019... etc
Totals<-Totals %>% mutate("Previous Years"=`2016`+`2017`+`2018`)
Now, I'm actually trying to set this up so that I can select this programmatically; next year, I'd rather look at 2017, 2018, and 2019 data, naturally, and I'd like to just set it up so that I can enter a year number, and it will select the right columns using code.
year = 2019
index<-which(colnames(Totals)==year)
Totals<-Totals%>%
##Here's where it gets hairy
mutate("Previous Years"=Totals[index-3]+Totals[index-2]+Totals[index-1])
Error: Column `Previous Years` is of unsupported class data.frame
So, there are some issues. Obviously, I'm calling something incorrectly, scenario 1 above works like a charm, and the second scenario gives an error. I feel like it has something to do with the back ticks that you would normally use to call columns with unusual names in dplyr.
What is the proper way to do something like this?
long_Totals <- gather(Totals, Year, Funding, starts_with("20")). - AxemanTotals[2018]to the console and see what it outputs. It won't give you a vector, which is the source of the error - Calum You