Skip to content
Back to formatted view

Raw Message

Message-ID: <BF88BD01-2708-4BB4-B1ED-6BDF3641F0D9@xs4all.nl>
Date: 2012-12-17T19:39:45Z
From: Berend Hasselman
Subject: Why does matrix selection behave differently when using which?
In-Reply-To: <CAPccJQHMfVvmp42uQxAV_BJ9t3BURu_R-UcAvFNeRsB+e5vpqQ@mail.gmail.com>

On 17-12-2012, at 20:22, Asis Hallab wrote:

> Dear R community,
> 
> I have a medium sized matrix stored in variable "t" and a simple function "
> countRows" (see below) to count the number of rows in which a selected
> column "C" matches a given value. If I count all rows matching all pairwise
> distinct values in the column "C" and sum these counts up, I get the number
> or rows of "t". If I delete the "which" calls from function "countRows" the
> resulting sum of matching row numbers is much greater than the number of
> rows in "t".
> 
> The table "t" I use can be downloaded from here:
> https://github.com/groupschoof/PhyloFun/archive/test_selector.zip
> Unzip the file and read in the table "t" using t <- read.table("test.tbl")
> 
> The above function "sumRows" is defined as follows:
> sumRows <- function( tbl, ps ) {
>  sum(
>    sapply(ps,
>      function(x) {
>        t <- if ( is.na(x) ) {
>          tbl[ which( is.na(tbl[ , "Domain.Architecture.Distance" ]) ), ,
> drop=F]
>        } else {
>          tbl[ which( tbl[ , "Domain.Architecture.Distance" ] == x ), ,
> drop=F]
>        }
>        nrow(t)
>      }
>    )
>  )
> }
> 

And how are we supposed to call sumRows()?

sumRows(???, ???

Berend