[R] Identifying and characterizing strings of NA in a vector
dimitris.rizopoulos at med.kuleuven.be
Thu Oct 11 15:59:42 CEST 2007
try something like this:
vec <- c(1, 2, 1, NA, NA, 1, 2, NA, NA, NA, 3, 4, NA, NA)
out <- rle(is.na(vec))
I hope it helps.
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
----- Original Message -----
From: "Marie-Pierre Sylvestre" <marie-pierre.sylvestre at mail.mcgill.ca>
To: <r-help at r-project.org>
Sent: Thursday, October 11, 2007 3:47 PM
Subject: [R] Identifying and characterizing strings of NA in a vector
> Dear R users,
> I was wondering if someone could suggest a few lines of code for my
> I want to count the number and the length of strings of NA in a
> For example:
> vec <- c(1, 2, 1, NA, NA, 1, 2, NA, NA, NA, 3, 4, NA, NA)
> has 2 strings of NA's of length 2 and 1 string of NA' of length 3.
> I can easily count the number of NA's per vector, but I am having a
> time counting the number and length of strings of NA's per vector
> without relying heavily on loops. I will have to perform this task
> many vectors.
> Can somebody help?
> many thanks,
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help