Dimitri Liakhovitski
2007-Nov-19 23:41 UTC
[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
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. >
You can use matrix by vector multiplication: m <- as.matrix(POPULATION) beta <- as.numeric(beta.vector) y <- m %*% beta POPULATION <- cbind(POPULATION,y) --- Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> 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. >