vioravis wrote on 12/06/2011 12:42:35 PM:

> I am trying to code the following excel formula in R. 
> 
> a            b          c            Result    Formula
> 1           10         0.1         #N/A 
> IF(B2<20,NA(),C2+IF(ISERROR(D1),0,D1))
> 2           20         0.2         0.2 
> IF(B3<20,NA(),C3+IF(ISERROR(D2),0,D2))
> 3           30         0.3         0.5 
> IF(B4<20,NA(),C4+IF(ISERROR(D3),0,D3))
> 4           40         0.4         0.9 
> IF(B5<20,NA(),C5+IF(ISERROR(D4),0,D4))
> 5           50         0.5         1.4 
> IF(B6<20,NA(),C6+IF(ISERROR(D5),0,D5))
> 6           60         0.6         2 
> IF(B7<20,NA(),C7+IF(ISERROR(D6),0,D6))
> 7           70         0.7         2.7 
> IF(B8<20,NA(),C8+IF(ISERROR(D7),0,D7))
> 8           80         0.8         3.5 
> IF(B9<20,NA(),C9+IF(ISERROR(D8),0,D8))
> 9           90         0.9         4.4 
> IF(B10<20,NA(),C10+IF(ISERROR(D9),0,D9))
> 10        100         1           5.4 
> IF(B11<20,NA(),C11+IF(ISERROR(D10),0,D10))
> 
> 
> The variable "Result" is obtained using the excel formula shown next to 
it.
> Column D contains the Result.
> 
> dataFrame <- data.frame(a = seq(1:10),b = seq(10,100,by = 10),c =
> seq(0.1,1,by = 0.1)) 
> 
> Can someone please help me as how to calculate the sequential sum in R 
given
> by the excel formula??
> 
> Thank you.
> 
> Ravi


Try this:

d <- ifelse(dataFrame$b < 20, 0, dataFrame$c)
dataFrame$result <- cumsum(d)
dataFrame$result[dataFrame$b < 20] <- NA

Jean
	[[alternative HTML version deleted]]

