[R] Effective code for 2 vector multiplication?

Peter Alspach PAlspach at hortresearch.co.nz
Tue Nov 20 00:51:19 CET 2007


Dimitri

Why not simply:

POPULATION$Y <- as.matrix(POPULATION)%*%beta.vector ?

Peter Alspach (VPN 31708)
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri 
> Liakhovitski
> Sent: Tuesday, 20 November 2007 12:42 p.m.
> To: R-Help List
> Subject: [R] Effective code for 2 vector multiplication?
> 
> Hello!
> 
> I have a data frame POPULATION populated with numeric values such that
> dim(POPULATION) are: 100,000 (rows) and 3 (columns).
> 
> Also, I have a vector beta.vector that has 3 elements (all 
> numeric values).
> 
> I want to create a new variable Y (a new column) in 
> POPULATION such that for each row of POPULATION, the value of 
> Y equals the product of
> 2 vectors: 3 values of the corresponding row of POPULATION 
> times beta.vector.
> 
> Here is the code I wrote:
> 
> POPULATION$Y<-sapply(as.list(1:nrow(POPULATION)), function(x) {
> x2<-as.numeric(POPULATION[x,]) %*% as.numeric(beta.vector)
> return(x2)
> } )
> 
> It calculates what I need and creates POPULATION$Y BUT - it 
> takes 50 seconds (!!!) My question: is there a more effective 
> (faster) way of doing what I am trying to do?
> 
> Thanks a lot!
> Dimitri
> 
> ______________________________________________
> 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