[R] count NAs per week

arun smartpink111 at yahoo.com
Mon Sep 17 14:28:40 CEST 2012


Hi,
Try this:
dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))
 aggregate(dattrial$a,list(dattrial$Week),function(x) sum(is.na(x)))
#  Group.1 x
#1       3 1
#2       4 0
#or
 ddply(dattrial,.(Week),summarize, sum(is.na(a))) 
#  Week ..1
#1    3   1
#2    4   0

#or
list1<-split(dattrial,dattrial$Week)
 unlist(lapply(lapply(list1,`[`, 1),function(x) sum(is.na(x))))
#3 4 
#1 0 
A.K. 





----- Original Message -----
From: Tagmarie <Ramgad82 at gmx.net>
To: r-help at r-project.org
Cc: 
Sent: Monday, September 17, 2012 6:03 AM
Subject: [R] count NAs per week

Even though I work with R since a year or so I still struggle with simple
problems. I hope someone can help me with this. Been trying for days and am
a little frustrated now. 

I have a data frame somewhat like the one bellow:

dattrial<-data.frame(a=c(1,NA,rnorm(4,10)), Week=c(3,3,3,4,4,4))

I want to know how many NAs I have in week 3 and in week 4. 



--
View this message in context: http://r.789695.n4.nabble.com/count-NAs-per-week-tp4643351.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.





More information about the R-help mailing list