Johannes Hüsing
2007-Aug-10 08:55 UTC
[R] [Fwd: Re: How to apply functions over rows of multiple matrices]
[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 matrixwhich 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 } applvec } 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. --
Gabor Grothendieck
2007-Aug-10 12:16 UTC
[R] [Fwd: Re: How to apply functions over rows of multiple matrices]
1. matrices are stored columnwise so R is better at column-wise operations than row-wise. 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)))) 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) 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? On 8/10/07, Johannes H?sing <johannes at huesing.name> wrote:> [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 > } > applvec > } > > 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. > > > > > > -- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Seemingly Similar Threads
- How to apply functions over rows of multiple matrices
- DOE and interaction plot general question
- Cut intervals (character) to numeric midpoint; regex problem
- Find last row (observation) for each combination of variables
- HELP: BRUGS/WinBUGS/RBUGS Response is a combination of random variables