[R] Help with vector gymnastics

Felix Andrews felix at nfrac.org
Wed Aug 22 17:31:12 CEST 2007


library(zoo)
tf <- c(T,F,F,F,T,T,F)
i <- seq(7)
answer <- ifelse(tf, i*5, NA)
answer <- na.locf(answer)


On 8/23/07, Gladwin, Philip <philip.gladwin at citi.com> 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.
>


-- 
Felix Andrews / 安福立
PhD candidate
Integrated Catchment Assessment and Management Centre
The Fenner School of Environment and Society
The Australian National University (Building 48A), ACT 0200
Beijing Bag, Locked Bag 40, Kingston ACT 2604
http://www.neurofractal.org/felix/
voice:+86_1051404394 (in China)
mobile:+86_13522529265 (in China)
mobile:+61_410400963 (in Australia)
xmpp:foolish.android at gmail.com
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8



More information about the R-help mailing list