Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander
Multiplying elements of a list by rows of a matrix
6 messages · Clemontina Davenport, Rui Barradas, Bert Gunter +2 more
Hello, Thanks for the data example. Try lapply(seq_along(X), function(i) tt[i,] %*% X[[i]]) Hope this helps, Rui Barradas Em 11-11-2012 16:33, Clemontina Davenport escreveu:
Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander
______________________________________________ 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.
Clemontina: As you have seen, the answer is yes, but my question is why bother? What's wrong with a for() loop? lapply() should offer no advantage in speed over a loop. Vectorization could, but lapply() is not vectorization. -- Bert
On Sun, Nov 11, 2012 at 8:33 AM, Clemontina Davenport <ckalexa2 at ncsu.edu> wrote:
Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander
______________________________________________ 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.
Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi, In this case, you could try: res<-lapply(mapply(c,X,lapply(data.frame(t(tt[1:3,])),function(x) x),SIMPLIFY=FALSE),function(x) x[13:16]%*% matrix(x[1:12],ncol=3) ) res #[[1]] ?# ??? [,1]? [,2]? [,3] #[1,] 14.27 16.65 10.12 #[[2]] #????? [,1] [,2]? [,3] #[1,] 10.14 5.17 18.28 # #[[3]] ?# ??? [,1]? [,2] [,3] #[1,] 22.66 10.34 8.31 A.K. ----- Original Message ----- From: Clemontina Davenport <ckalexa2 at ncsu.edu> To: r-help at r-project.org Cc: Sent: Sunday, November 11, 2012 11:33 AM Subject: [R] Multiplying elements of a list by rows of a matrix Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander ______________________________________________ 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.
Hello, I don't think he advantage here is speed but simplicity, lapply does it in one line of code. Rui Barradas Em 11-11-2012 18:02, Bert Gunter escreveu:
Clemontina: As you have seen, the answer is yes, but my question is why bother? What's wrong with a for() loop? lapply() should offer no advantage in speed over a loop. Vectorization could, but lapply() is not vectorization. -- Bert On Sun, Nov 11, 2012 at 8:33 AM, Clemontina Davenport <ckalexa2 at ncsu.edu> wrote:
Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander
______________________________________________ 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.
lapply(mapply(lapply(...))), along with making a vector out of a matrix then making a matrix out of it again seems like a pretty long-winded way of doing what is done by lapply(seq_along(X), function(i)tt[i,] %*% X[[i]]) or mapply(`%*%`, split(tt,row(tt))[1:3], X, SIMPLIFY=FALSE) (The example datasets are odd - why doesn't tt have the same number of rows as X has matrices?) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of arun Sent: Sunday, November 11, 2012 10:41 AM To: Clemontina Davenport Cc: R help Subject: Re: [R] Multiplying elements of a list by rows of a matrix Hi, In this case, you could try: res<-lapply(mapply(c,X,lapply(data.frame(t(tt[1:3,])),function(x) x),SIMPLIFY=FALSE),function(x) x[13:16]%*% matrix(x[1:12],ncol=3) ) res #[[1]] ?# ??? [,1]? [,2]? [,3] #[1,] 14.27 16.65 10.12 #[[2]] #????? [,1] [,2]? [,3] #[1,] 10.14 5.17 18.28 # #[[3]] ?# ??? [,1]? [,2] [,3] #[1,] 22.66 10.34 8.31 A.K. ----- Original Message ----- From: Clemontina Davenport <ckalexa2 at ncsu.edu> To: r-help at r-project.org Cc: Sent: Sunday, November 11, 2012 11:33 AM Subject: [R] Multiplying elements of a list by rows of a matrix Hi all, I have the following code: set.seed(1) x1 <- matrix(sample(1:12), ncol=3) x2 <- matrix(sample(1:12), ncol=3) x3 <- matrix(sample(1:12), ncol=3) X <- list(x1,x2,x3) tt <- matrix(round(runif(5*4),2), ncol=4) Is there a way I can construct a new list where newlist[[i]] = tt[i,] %*% X[[i]] without using a for loop? Each element of newlist will be 3 x 1 vector. Thanks -- Tina Alexander
______________________________________________ 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. ______________________________________________ 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.