Re: [R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Krishna Tateneni
Many thanks to all who responded, and especially for the promptness of your answers. The common thread in your solutions is the use of rle, a very useful function of which I was ignorant. --Krishna [[alternative HTML version deleted]] __ R-hel

Re: [R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Marc Schwartz
On Jul 7, 2009, at 4:08 PM, Krishna Tateneni 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 cr

Re: [R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Stavros Macrakis
Here's one possibility: vv <- c(10,8,1,3,0,8,NA,NA,NA,NA,2,1,6,NA,NA,NA,0,5,1,9) > (1+cumsum(diff(is.na(c(vv[1],vv)))==1)) * !is.na(vv) [1] 1 1 1 1 1 1 0 0 0 0 2 2 2 0 0 0 3 3 3 3 On Tue, Jul 7, 2009 at 5:08 PM, Krishna Tateneni wrote: > Greetings, I have a vector of the form: > [10,8,1,3,0,

Re: [R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Jorge Ivan Velez
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

[R] Numbering sequences of non-NAs in a vector

2009-07-07 Thread Krishna Tateneni
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