Conditional sum based on indicator
data-manipulation, r, sum
Solution
Another option using `I`
sum(with(dat,A*I(Ind==1)+B*(Ind==2)))
Problem
I have a data frame like this: ``` A B Ind 1 10 8 1 2 9 10 2 3 7 1 2 4 19 20 1 5 . . . ``` How can I sum the columns based on the `Ind` value? If `Ind==1`, sum from column `A`, if `Ind==2`, sum from column `B`. For example, the output for the first 4 rows should be `10+10+1+19=30`, where the first 10 is in `A`, second 10 is in `B`, third 1 is in `B` and fourth 19 is in `A`.