0

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

Amit Mishra
  • 33
  • 1
  • 10
  • Suppose you have two sets of `1 1 1` and `1 1 1 1 1` in a row. What would be the expected value? It would have been helpful if you show the expected result based on the example data. – akrun May 02 '15 at 12:38
  • 1
    Not much clear, but are you looking for something like `apply(inter_cv_vars,1,function(x) max(cumsum(x)*cumprod(x)))`? – nicola May 02 '15 at 12:46
  • @arun: result would be 3 & 5 for the column "O_ZERO_BILLING_CNT_TRL_Y1" but if the value will be 111101 then it will be 4 , i.e continous value of "1" in a row. – Amit Mishra May 02 '15 at 12:49
  • I am not sure I understand this properly. Perhaps, `apply(inter_cv_vars,1,function(x) with(rle(!!x), lengths[values]))` – akrun May 02 '15 at 12:52
  • @nicola : Thanks a lot :).....Ya that's the exact function i was looking for ......thanks again.... – Amit Mishra May 02 '15 at 12:56

1 Answers1

2

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
nicola
  • 24,005
  • 3
  • 35
  • 56