[R] Multiplying elements of a list by rows of a matrix

Rui Barradas ruipbarradas at sapo.pt
Sun Nov 11 20:22:58 CET 2012


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.
>
>




More information about the R-help mailing list