s-news
[Top] [All Lists]

Re: tapply question

To: "Michael Slattery" <Michael.Slattery@epa.state.oh.us>, <s-news@lists.biostat.wustl.edu>
Subject: Re: tapply question
From: "David Huffer" <David.Huffer@csosa.gov>
Date: Thu, 6 Mar 2008 11:43:56 -0500
Thread-index: Ach/lOsidv3zpy1BRM6W5ulgidCX/wAD9nEg
Thread-topic: [S] tapply question
On Thursday, March 06, 2008 9:16, Michael Slattery wrote:

 > ...I have a tapply problem, which I  thought
 > was  an  easy  one,  but  the  solution  has
 > nonetheless eluded  me  (more  coffee?).  My
 > dataframe structure is this:...
 >
 >   index    R1            R2   ...      R40
 >   A        NA            345.6         .
 >   C        non-detect    non-detect    .
 >   B        1.03          NA            .
 >   B        1.55          NA            .
 >   A        NA            234.5         .
 >   C        non-detect    NA            .
 >   .        .             .             .
 >   .        .             .             .
 >   .        .             .             .
 > ...What I need are simultaneous counts of 1)
 > NA's and 2) non-detect's for each column Rx,
 > for each index (n=3), across some 40 columns
 > and some 150K records...

Would something simple like this work:

   > ctNAND <- function ( x ) {
   +   cbind (
   +     sumNA  = sum ( x == "NA" )
   +     , sumND  = sum ( x == "non-detect" )
   +   )
   + }
   >
   > lapply (
   +   list ( R1 , R2 , R3 )
   +   , function ( x ) tapply (
   +     x
   +     , index
   +     , ctNAND
   +   )
   + )
   [[1]]:
   $a:
        sumNA sumND
   [1,]     8     3

   $b:
        sumNA sumND
   [1,]    10     4

   $c:
        sumNA sumND
   [1,]     8     3


   [[2]]:
   $a:
        sumNA sumND
   [1,]     7     2

   $b:
        sumNA sumND
   [1,]    11     5

   $c:
        sumNA sumND
   [1,]     9     1


   [[3]]:
   $a:
        sumNA sumND
   [1,]     8     2

   $b:
        sumNA sumND
   [1,]     7     4

   $c:
        sumNA sumND
   [1,]     7     2

   >

--
 David Huffer

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