[R] Help with vector gymnastics

Erik Iverson iverson at biostat.wisc.edu
Wed Aug 22 17:33:37 CEST 2007


Philip -

I don't know if this is the "best" way, but it gives you the output you 
want.

Using your tf,

vals <- rle(ifelse(tf, 5*which(tf), 0))
vals$values[vals$values == 0] <- vals$values[which(vals$values==0) - 1]
inverse.rle(vals)
[1]  5  5  5  5 25 30 30

Gladwin, Philip wrote:
> Hello,
> 
> What is the best way of solving this problem?
> 
> answer <- ifelse(tf=TRUE, i * 5, previous answer)
> where as an initial condition 
> tf[1] <- TRUE
> 
> 
> For example if,
> tf <- c(T,F,F,F,T,T,F)
> over i = 1 to 7
> then the output of the function will be
> answer = 5 5 5 5 25 30 30 
> 
> Thank you.
> 
> Phil,
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list