[R] count NAs per week

R. Michael Weylandt michael.weylandt at gmail.com
Mon Sep 17 13:24:35 CEST 2012


On Mon, Sep 17, 2012 at 11:03 AM, Tagmarie <Ramgad82 at gmx.net> wrote:
> 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.
>
>

Thanks for the reproducible example: there are many ways to do this
(aggregate, tapply, ave, etc.) but they are all based on the paradigm
of: break up your data by "Week" --> apply the function "function(x)
sum(is.na(x))" --> recombine. (See, inter alia, the JSS paper on the
plyr package) I'm not at a computer with R right now so this is a
little untested, but one way might be:

with(dattrial, tapply(a, Week, function(x) sum(is.na(x))))

Cheers,
Michael

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