[R] Simple Missing cases Function

From: Tim Elwell-Sutton <tesutton_at_hku.hk>
Date: Tue, 19 Apr 2011 15:29:08 +0800

Dear all  

I have written a function to perform a very simple but useful task which I do regularly. It is designed to show how many values are missing from each variable in a data.frame. In its current form it works but is slow because I have used several loops to achieve this simple task.  

Can anyone see a more efficient way to get the same results? Or is there existing function which does this?  

Thanks for your help



miss <- function (data)


    miss.list <- list(NA)

    for (i in 1:length(data)) {

        miss.list[[i]] <- table(is.na(data[i]))


    for (i in 1:length(miss.list)) {

        if (length(miss.list[[i]]) == 2) {

            miss.list[[i]] <- miss.list[[i]][2]



    for (i in 1:length(miss.list)) {

        if (names(miss.list[[i]]) == "FALSE") {

            miss.list[[i]] <- 0



    data.frame(names(data), as.numeric(miss.list))




     data.m <- ToothGrowth

     data.m$supp[sample(1:nrow(data.m), size=25)] <- NA


        [[alternative HTML version deleted]]

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. Received on Tue 19 Apr 2011 - 07:33:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 19 Apr 2011 - 08:10:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive