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
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
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,
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
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
5 matches
Mail list logo