Amit Mishra
Amit Mishra

Reputation: 33

R : Taking sum of row on a condition in a loop

I have a dataframe which looks somewhat like this :

inter_cv_vars <- data.frame( 
I_Org_ZB1 = rep(0:1,each = 2, len = 20),
I_Org_ZB2 = rep(0:1,each = 1, len = 20),
I_Org_ZB3 = rep(0:1,each = 3, len = 20),
I_Org_ZB4 = rep(0:1,each = 4, len = 20),
I_Org_ZB5 = rep(0:1,each = 5, len = 20),
I_Org_ZB6 = rep(0:1,each = 1, len = 20),
I_Org_ZB7 = rep(0:1,each = 3, len = 20),
I_Org_ZB8 = rep(0:1,each = 2, len = 20),
I_Org_ZB9 = rep(0:1,each = 4, len = 20),
I_Org_ZB10 = rep(0:1,each = 6, len = 20),
I_Org_ZB11 = rep(0:1,each = 2, len = 20),
I_Org_ZB12 = rep(0:1,each = 1, len = 20))

I want to create a new column "O_ZERO_BILLING_CNT_TRL_Y1" in this dataframe based on a condition that:

The summation of the continuous "1" value starting from column "I_Org_ZB1", if the sequence break then the value of the column "O_ZERO_BILLING_CNT_TRL_Y1" will be the sequence till the continous "1" value occured.

Eg:- If value of I_Org_ZB1 I_Org_ZB2 I_Org_ZB3 is 1,1,1 n every other value is zero then the column "O_ZERO_BILLING_CNT_TRL_Y1" value will be 3 but if the value is 1,0,1 & every other column is 0 then the value of "O_ZERO_BILLING_CNT_TRL_Y1" will be 1. If in a particular row all value is 1 then the value of column will be 12.

I have tried the below code:

for (i in 1:12){

if(i == 12)
{next}

ifelse(inter_cv_vars$I_Org_ZB1 == 1,

inter_cv_vars$O_ZERO_BILLING_CNT_TRL_Y1 <- ifelse(

rowSums(inter_cv_vars[,1:eval(parse(text=sprintf("(12-%s+1)",i)))]) == eval(parse(text=sprintf("(12-%s+1)",i))),

eval(parse(text=sprintf("(12-%s+1)",i))),0),

0)

}

But getting a wrong answer, could anyone point out the mistake which I made or provide any other alternative.

Thanks in advance your help :)

Regards,

Amit

Upvotes: 0

Views: 794

Answers (1)

nicola
nicola

Reputation: 24480

This is far from being optimized, but it should work if I got what you need:

apply(inter_cv_vars,1,function(x) max(cumsum(x)*cumprod(x)))
#[1] 0 0 1 3 0 0 1 2 0 0 1 3 0 0 1 9 0 0 1 2

Upvotes: 2

Related Questions