Given the following data:
df <- data.frame(x1 = c(1,2,3,4,5),
x2 = c(5,4,3,2,1))
I would like to mutate a new column (x3) which is the sum of x1 and x2 columns. However, I would like to conditionally replace the values before calculating such that:
1 = 0
2 = 1
3 = 2
4 = 3
5 = 3
This would result in the following mutated column:
x3
3
4
4
4
3
My solution so far has involved mutating new x1 and x2 columns containing the replacement values before summing those and then dropping the mutated x1 and x2 but this seems very inefficient.