I have the below dataframe which contains number of products sold in each quarter by a salesman.
Ag Q1 Q2 Q3 Q4
s121 4 3 0 0
S431 0 0 2 1
S246 0 0 0 2
S444 2 2 2 2
I am trying to create a calculated column C which is basically sum of all columns where the value is not zero. So basically number of quarters a salesman has been active.
Expected output:
Ag Q1 Q2 Q3 Q4 C
s121 4 3 0 0 2
S431 0 0 2 1 2
S246 0 0 0 2 1
S444 2 2 2 2 4
I have tried
df$C <- df[rowSums(df[,c(Q1,Q2,Q3,Q4)])]
but this gives undefined columns selected error. Can someone please help me rectify this?