Another way: a = c(1,NA,NA,4,3,NA,NA,NA,NA,5)
# find position of the non-NAs in the vector pos = which(!is.na(a)) # this calculates the length by taking the differences between the non-NA positions. diff(pos)-1 #get the max max(diff(pos)-1) Richard On Tue, Jan 7, 2014 at 7:36 PM, arun <smartpink...@yahoo.com> wrote: > Hi, > Try: > rl <- rle(is.na(a)) > max(rl$lengths[rl$values]) > #[1] 3 > > > A.K. > > > Hi, > > I'd like to detect whether a vector contains a sequence of NA values of a > certain length. So, for example, if I have a vector a = > c(1,NA,NA,4,NA,NA,NA,5); how can I find what the longest sequence of NAs is > (in this case 3)? > > Note that this is different from simply summing the number of NA values in > the vector (which would be 5 in this case)... > > Thanks for your help ! > > ______________________________________________ > 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. ______________________________________________ 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.