s-news
[Top] [All Lists]

Re: apply

To: v.moreno@ico.scs.es
Subject: Re: apply
From: Tim Hesterberg <timh@insightful.com>
Date: Mon, 27 Oct 2003 15:09:27 -0800
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <200310241335.09585.v.moreno@ico.scs.es> (message from Victor Moreno on Fri, 24 Oct 2003 13:35:09 +0200)
References: <200310241335.09585.v.moreno@ico.scs.es>
>I want to count the number of missing cases for each variable in a data.frame
>
>apply( data,2,function(x)sum(is.na(x))) returns always 0
>lapply( data,function(x)sum(is.na(x))) returns a list with correct answers
>sapply( data,function(x)sum(is.na(x))) returns a vector with correct answers
>
>Why is apply not working in this situation?

As Spencer Graves pointed out, because if there are character or factor
columns, the data frame is converted to a character matrix.
To avoid this, handle the data frame as a list, and use sapply.

Two alternatives are:
        sapply(data, function(x) sum(is.na(x)))
        sapply(data, function(x) length(which.na(x)))
The latter should be more efficient.

My favorite:
        library(missing)  # do this once in a session
        sapply(data, numberMissing)

Tim Hesterberg

========================================================
| Tim Hesterberg       Research Scientist              |
| timh@insightful.com  Insightful Corp.                |
| (206)802-2319        1700 Westlake Ave. N, Suite 500 |
| (206)802-2500 (fax)  Seattle, WA 98109-3044, U.S.A.  |
|                      www.insightful.com/Hesterberg   |
========================================================

<Prev in Thread] Current Thread [Next in Thread>