[R] Simple Missing cases Function

Tim Elwell-Sutton tesutton at hku.hk
Tue Apr 19 10:18:27 CEST 2011


Dear Petr
Thanks so much. That is a LOT more efficient.
Tim

-----Original Message-----
From: Petr PIKAL [mailto:petr.pikal at precheza.cz] 
Sent: Tuesday, April 19, 2011 3:37 PM
To: tesutton
Cc: r-help at r-project.org
Subject: Odp: [R] Simple Missing cases Function

Hi

Hi
try

colSums(is.na(data.m))

It is not in data frame but you can easily transform it if you want.

Regards
Petr


r-help-bounces at r-project.org napsal dne 19.04.2011 09:29:08:

> 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
> 
> Tim
> 
> 
> 
> Function:
> 
> 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))
> 
> }
> 
> 
> 
> Example:
> 
> data(ToothGrowth)
> 
>      data.m <- ToothGrowth
> 
>      data.m$supp[sample(1:nrow(data.m), size=25)] <- NA
> 
>      miss(data.m)
> 
> 
>    [[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.



More information about the R-help mailing list