Question

I'm new to R programming and I know I could write a loop to do this, but everything I read says that for simplicity its best to avoid loops and use apply instead.

I have a matrix and i would like to run this function on each element in the matrix.

cellresidue <- function(i,j){
  result <- (cluster[i,j] - cluster.I[i,] - cluster.J[j,] - cluster.IJ)/(cluster.N*cluster.M)
  return (result)
}

i= element row
j= element column
cluster.J is a matrix of column means
cluster.I is a matrix of row means
cluster.IJ is the mean of the entire matrix named cluster

What I can't figure out is how do I get the row and column of the element (I think should use row() and column col() functions) that mapply is working with and how do pass those arguments to mapply or apply?

Was it helpful?

Solution

There is no need for loops or *apply functions. You can just use plain matrix operations:

nI <- nrows(cluster)
nJ <- ncols(cluster)
cluster.I  <- matrix(rowMeans(cluster), nI, nJ, byrow = FALSE)
cluster.J  <- matrix(rowMeans(cluster), nI, nJ, byrow = TRUE)
cluster.IJ <- matrix(    mean(cluster), nI, nJ)

residue.mat <- (cluster - cluster.I - cluster.J - cluster.IJ) /
               (cluster.N * cluster.M)

(You did not explain what cluster.N and cluster.M are but I assume they are scalars)

OTHER TIPS

It is not clear from your question what you are trying to do. It is best on this site to provide some mock data (preferably generated by the code, not pasted), and then show what form the end result should look like. It seems that the apply family is not what you seek.

Quick disambiguation between apply, sapply and mapply:

#providing data for examples
X=matrix(rnorm(9),3,3)

apply: apply a function to either columns (2) or rows (1) of a matrix or array

#here, sum by columns, same as colSums(X)
apply(X, 2, sum)

sapply: apply a function against (usually) a list of objects

#create a list with three vectors
mylist=list(1:4, 5:10, c(1,1,1))
#get the mean of each vector
sapply(mylist, mean)

#remove 2 to each element of X, same as c(X-2)
sapply(X, FUN=function(x) x-2)

mapply: a multivariate version of sapply, taking an arbitrary number of arguments. Never had much use of it… Some rock-bottom examples:

#same as c(1,2,3,4) + c(15,16,17,18)
mapply(sum, 1:4, 15:18)

#same as c(X+X), the vectorized matrix sum
mapply(sum, X, X)

Side note: It's perfectly ok to use loops in R; use whichever suits the best your thoughts. The issue is that if you have a "really big" number of iterations, this is where you could meet bottlenecks, depending on your patience. There are two solutions to this: rewrite your function in C/FORTRAN (and boost speed), or use built-in functions if applicable (which are, by the way, often writen in C or FORTRAN).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top