Why does matrix selection behave differently when using which?
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