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,
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
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.
try 5*which(tf)[cumsum(tf)] Gladwin, Philip schrieb:> 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. >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 22046 Hamburg T ++49/40/42803-8243 F ++49/40/42803-7790