I have a dataset and I want to sum all the columns values after I spread them.
For example, if I have the data.frame:
data.frame(
country = c('US','US','Brazil','Brazil','Canada'),
variable = c('v1','v2','v1','v3','v4'),
value = c(1,2,3,4,5)
) %>%
spread(variable, value, fill = 0)
It results in:
country v1 v2 v3 v4
1 Brazil 3 0 4 0
2 Canada 0 0 0 5
3 US 1 2 0 0
I want it to finish like this:
country v1 v2 v3 v4 total
1 Brazil 3 0 4 0 7
2 Canada 0 0 0 5 5
3 US 1 2 0 0 3
Normally a simple mutate(total = v1 + v2 + v3 + v4)
would solve the problem, but, in my case, I have no prior knowledge of the columns names.
How can I create this new column?