[R] [Fwd: Re: How to apply functions over rows of multiple matrices]

Johannes Hüsing johannes at huesing.name
Fri Aug 10 10:55:22 CEST 2007

[Apologies to Gabor, who I sent a personal copy of the reply
erroneously instead of posting to List directly]

>  Perhaps what you really intend is to
> take the average over those elements in each row of the first matrix
which correspond to 1's in the second in the corresponding
> row of the second.  In that case its just:
> rowSums(newtest * goldstandard) / rowSums(goldstandard)

Thank you for clearing my thoughts about the particular example.
My question was a bit more general though, as I have different
functions which are applied row-wise to multiple matrices. An
example that sets all values of a row of matrix A to NA after the
first occurrence of TRUE in matrix B.

fillfrom <- function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec <- applvec
  if (length(testvec) != length(applvec)) {
    stop("applvec and testvec have to be of same length!")
  } else if(any(testvec, na.rm=TRUE)) {
    applvec[min(which(testvec)) : length(applvec)] <- NA

fillafter <- function(applvec, testvec=NULL) {
  if (is.null(testvec)) testvec <- applvec
  fillfrom(applvec, c(FALSE, testvec[-length(testvec)]))

numtest <- 6
numsubj <- 20

newtest <- array(rbinom(numtest*numsubj, 1, .5),
    dim=c(numsubj, numtest))
goldstandard <- array(rbinom(numtest*numsubj, 1, .5),
    dim=c(numsubj, numtest))

newtest.NA <- t(sapply(1:nrow(newtest), function(i) {
  fillafter(newtest[i,], goldstandard[i,]==1)}))

My general question is if R provides some syntactic sugar
for the awkward sapply(1:nrow(A)) expression. Maybe in this
case there is also a way to bypass the apply mechanism and
my way of thinking about the problem has to be adapted. But
as the *apply calls are galore in R, I feel this is a standard
way of dealing with vectors and matrices.


More information about the R-help mailing list