[R] count NAs with aggregate

Rui Barradas ruipbarradas at sapo.pt
Mon Mar 25 17:34:47 CET 2013


Hello,

The problem seems to be with the formula interface.

# Make up some data
var1 <- rnorm(100)
var2 <- rnorm(100)
var3 <- rnorm(100)
subject <- sample(4, 100, replace = TRUE)
time <- sample(10, 100, replace = TRUE)
var1[sample(100, 10)] <- NA
var2[sample(100, 10)] <- NA
var3[sample(100, 10)] <- NA

mydataset <- data.frame(var1, var2, var3, subject, time)

count_nas <- function(arg1) {
   return(sum(is.na(arg1)))
}

aggregate(mydataset[, 1:3], list(mydataset$subject, mydataset$time), FUN 
= count_nas)


Hope this helps,

Rui Barradas

Em 25-03-2013 15:40, Timo Stolz escreveu:
> Dear members of this list,
>
> I'd like to count missing values using the aggregate function.
>
> Something like this:
>
> count_nas <- function(arg1) {
>    return(sum(is.na(arg1)))
> }
>
> aggregate(cbind(var1, var2, var3) ~ subject + time, data = mydataset,
> count_nas)
>
>
> It's not working: I end up with a matrix containing zeros, although
> there are missings in the data frame.
> I'd highly appreciate a hint!
>
> Thanks a lot,
> Timo Stolz
>
> ______________________________________________
> 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