[R] Issue with a function

Yara Abu Awad yara_abuawad at yahoo.com
Tue Sep 30 02:03:30 CEST 2014


Hi,

I'm trying to use a function to multiply a matrix (newX) with a simple list (beta).
I know that I can simply use matrix multiplication i.e. newX %*%as.matrix(beta)
and this works fine. However, the matrix multiplication gives me the sum of each row. What I really need are the new values of all the columns in newX if they were multiplied by beta (so the first column of newX should be multiplied by the first value in beta, second column by the second value etc..)

This is the function I'm trying to use:

Terms <-NULL
count <- 0

for (i in 1: ncol(newX)){
  for (j in 1: length(beta)){
    Terms <- cbind (Terms,(newX[,i]*beta[j]))
  }
}

I don't get an error message, however the function runs for a long time (about an hour) without resolving and I end up stopping it.



Now, if I multiply every individual column by each value of beta, the calculation works (code is below):
newX[,1]*beta[1]
newX[,2]*beta[2]

but since newX has 520 columns and beta has 520 values, I'd rather use a shortcut!!


I can't figure out why the above function doesn't work. I've used it to multiply two matrices and it works just fine.
Any insight would be appreciated.

Yara



More information about the R-help mailing list