[R] multiplying list with vector

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Sep 29 11:50:45 CEST 2011


One option is mapply(), e.g.,

mylist <- list("v1"=c(1,2,2,1), "v2"=c(2,2,2,1), "v3"=c(1,1,1,1))
myvector <- c(100, 10000, 1000000)

mapply("*", mylist, myvector)
mapply("*", mylist, myvector, SIMPLIFY = FALSE)


I hope it helps.

Best,
Dimitris



On 9/29/2011 11:43 AM, Marion Wenty wrote:
> hello everybody,
>
> i have got a question about lists:
>
> i have got the following commands:
>
> mylist<- list("v1"=c(1,2,2,1),"v2"=c(2,2,2,1),"v3"=c(1,1,1,1))
> myvector<- c(100,10000,1000000)
>
> now i would like to multiply each element of the list with the corresponding
> element of the vector, that is to say:
> v1*100
> v2*10000
> v3*1000000
> i only could think of lapply which can only be used to multiply each element
> of the list with the same number, i think.
>
> thank you very much in advance for your help,
> marion
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list