Dear Krishna, Here is one way. It is not very elegant, but seems to work: # x is the vector you want to change foo <- function(x){ R1 <- rle(!is.na(x)) R2 <- rle(is.na(x)) len <- R1$lengths[!R2$values] x[!is.na(x)] <- rep(1:length(len), len) x }
# Example x <- c(10, 8, 1, 3, 0, 8, NA, NA, NA, NA, 2, 1, 6, NA, NA, NA, 0, 5, 1, 9) foo(x) # [1] 1 1 1 1 1 1 NA NA NA NA 2 2 2 NA NA NA 3 3 3 3 HTH, Jorge On Tue, Jul 7, 2009 at 5:08 PM, Krishna Tateneni <taten...@gmail.com> wrote: > Greetings, I have a vector of the form: > [10,8,1,3,0,8,NA,NA,NA,NA,2,1,6,NA,NA,NA,0,5,1,9...] That is, a > combination > of sequences of non-missing values and missing values, with each sequence > possibly of a different length. > > I'd like to create another vector which will help me pick out the sequences > of non-missing values. For the example above, this would be: > [1,1,1,1,1,1,NA,NA,NA,NA,2,2,2,NA,NA,NA,3,3,3,3...]. The goal ultimately > is > to calculate means separately for each sequence. > > Your help is appreciated. If I'm making this more complicated than > necessary, I'd appreciate knowing that as well! > > Many thanks. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org 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.