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

Johannes Hüsing johannes at huesing.name
Fri Aug 10 15:24:34 CEST 2007


> 1. matrices are stored columnwise so R is better at column-wise operations
> than row-wise.

I am seeing this by my code which contains more t() than
what seems healthy. However, the summaries are patient-wise
over repeated measurements. Out of convention, I am storing
patients in rows and measurements in columns.

>
> 2. Here is one way to do it (although I am not sure its better than the
> index approach):
>
>    row.apply <- function(f, a, b)
>       t(mapply(f, as.data.frame(t(a)), as.data.frame(t(b))))
>

Ah, thank you so much. I'll take the generalization to N arguments
à la mapply() as an exercise for the reader.

> 3. The code for the example in this post could be simplified to:
>
>     first.1 <- apply(cbind(goldstandard, 1), 1, which.max)
>     ifelse(col(newtest) > first.1, NA, newtest)
>

Ouch! Consider this scholar slapped.

> 4. given that both examples did not inherently need row by row operations
>    I wonder if that is the wrong generalization in the first place?
>

Given that you managed to squeeze my 20 lines of code into 2 lines
AND that row.apply() does not exist in base without many people
missing it, I'll have to concede this point and eliminate the
craving for row.apply() in favour of the whole-object approach.



More information about the R-help mailing list