Marie-Pierre Sylvestre
2007-Oct-11 13:47 UTC
[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 problem. I want to count the number and the length of strings of NA in a vector. 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 hard 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 for many vectors. Can somebody help? many thanks, Marie-Pierre
Dimitris Rizopoulos
2007-Oct-11 13:59 UTC
[R] Identifying and characterizing strings of NA in a vector
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))
table(out$lengths[out$values])
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- 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
> problem.
>
> I want to count the number and the length of strings of NA in a
> vector.
> 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
> hard
> 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
> for
> many vectors.
>
> Can somebody help?
>
> many thanks,
>
> Marie-Pierre
>
> ______________________________________________
> R-help at 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Jorge Ivan Velez Valbuena
2007-Oct-11 21:01 UTC
[R] Identifying and characterizing strings of NA in a vector
Hi Marie-Pierre, You first should identify wich components of vec are NA, then compute the length. vec <- c(1, 2, 1, NA, NA, 1, 2, NA, NA, NA, 3, 4, NA, NA) # Which component is NA? wcs<-which(-is.na(vec)) # Number of NA's in vector vec length(wcs) If you need to do the same for many vector, you could try # A simple function to determinate the number of NA components in a vector x count.NAS=function(x) length(which(is.na(x))) # For vec count.NAS(vec) If you have many vector (for example in a matrix), you could use> apply(X,2,count.NAS)Best wishes, Jorge Ivan Velez Valbuena Industrial Engineer Master in Sciences (Candidate) Faculty of Sciences School of Statistics National University of Colombia at Medellin Medellin, Antioquia, Colombia e-mail: jivelez at unal.edu.co ----- Original Message ----- From: Marie-Pierre Sylvestre <marie-pierre.sylvestre at mail.mcgill.ca> Date: Thursday, October 11, 2007 9:47 am 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 problem. > > I want to count the number and the length of strings of NA in a > vector. > 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 hard > 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 > for > many vectors. > > Can somebody help? > > many thanks, > > Marie-Pierre > > ______________________________________________ > R-help at 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.htmland provide commented, minimal, self-contained, > reproducible code. >