[R] Issue with a function
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Tue Sep 30 03:47:46 CEST 2014
library(Matrix)
result <- newX %*% Diagonal( unlist(beta) )
Is beta really a list, or is it a vector? The str function can help with this... in most cases it is a numeric mode vector and the unlist is not needed.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On September 29, 2014 5:03:30 PM PDT, Yara Abu Awad <yara_abuawad at yahoo.com> wrote:
>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
>
>______________________________________________
>R-help at r-project.org 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.
More information about the R-help
mailing list