How to multiply all values in a list by a number in R?

R ProgrammingServer Side ProgrammingProgramming

To multiply all values in a list by a number, we can use lapply function. Inside the lapply function we would need to supply multiplication sign that is * with the list name and the number by which we want to multiple all the list values. For example, if we have a list called LIST and we want to multiply each value in LIST by 2 then it can be done by using the command lapply(LIST,"*",2).

Example1

 Live Demo

List1<−list(x1=rpois(100,5),x2=rpois(100,2),x3=rpois(100,2),x4=rpois(100,3))
List1

Output

$x1
[1] 4 4 1 4 7 4 8 4 8 7 7 6 3 8 6 5 5 4 9 4 4 7 2 8 8
[26] 8 3 3 7 2 7 9 6 9 7 4 2 8 1 10 3 5 8 6 5 3 2 8 7 4
[51] 4 3 3 3 12 8 1 1 2 9 3 6 2 2 2 12 3 6 7 3 2 6 10 6 8
[76] 3 2 2 8 4 6 4 4 5 6 3 2 6 4 6 3 7 8 8 3 8 2 3 6 2
$x2
[1] 3 1 0 1 4 2 2 0 2 0 2 0 0 1 1 1 2 3 3 4 1 0 3 7 1 0 0 0 1 0 2 2 2 3 2 2 1
[38] 6 1 2 5 1 2 2 0 0 4 4 2 1 1 4 4 4 2 2 3 0 3 1 3 2 2 0 4 3 1 1 2 2 3 0 4 3
[75] 1 1 1 2 2 3 3 3 0 0 1 1 1 1 2 2 2 6 3 2 1 2 4 0 2 4
$x3
[1] 0 1 3 0 4 1 3 0 1 1 1 1 1 2 1 2 1 2 4 2 3 2 2 1 1 1 2 1 1 3 1 3 0 2 2 0 1
[38] 1 3 1 3 0 1 0 0 2 2 2 2 3 3 5 2 3 3 2 4 4 1 4 1 1 3 3 2 0 3 3 0 4 6 1 1 2
[75] 2 3 1 2 0 0 2 2 1 1 0 1 1 0 2 2 1 0 1 1 1 4 5 0 1 1
$x4
[1] 3 5 2 3 3 6 4 3 1 2 2 4 3 3 1 3 4 2 3 3 3 4 1 5 3 3 1 4 4 2 3 4 5 2 3 4 5
[38] 5 2 1 2 2 2 3 7 2 3 1 2 3 0 7 0 5 4 5 0 2 3 3 2 6 2 4 6 1 4 2 3 1 2 5 5 4
[75] 4 1 2 1 3 3 1 2 1 2 3 1 3 4 2 4 5 0 2 3 2 0 2 3 1 4

Example

lapply(List1,"*",2)

Output

$x1
[1] 8 8 2 8 14 8 16 8 16 14 14 12 6 16 12 10 10 8 18 8 8 14 4 16 16
[26] 16 6 6 14 4 14 18 12 18 14 8 4 16 2 20 6 10 16 12 10 6 4 16 14 8
[51] 8 6 6 6 24 16 2 2 4 18 6 12 4 4 4 24 6 12 14 6 4 12 20 12 16
[76] 6 4 4 16 8 12 8 8 10 12 6 4 12 8 12 6 14 16 16 6 16 4 6 12 4
$x2
[1] 6 2 0 2 8 4 4 0 4 0 4 0 0 2 2 2 4 6 6 8 2 0 6 14 2
[26] 0 0 0 2 0 4 4 4 6 4 4 2 12 2 4 10 2 4 4 0 0 8 8 4 2
[51] 2 8 8 8 4 4 6 0 6 2 6 4 4 0 8 6 2 2 4 4 6 0 8 6 2
[76] 2 2 4 4 6 6 6 0 0 2 2 2 2 4 4 4 12 6 4 2 4 8 0 4 8
$x3
[1] 0 2 6 0 8 2 6 0 2 2 2 2 2 4 2 4 2 4 8 4 6 4 4 2 2
[26] 2 4 2 2 6 2 6 0 4 4 0 2 2 6 2 6 0 2 0 0 4 4 4 4 6
[51] 6 10 4 6 6 4 8 8 2 8 2 2 6 6 4 0 6 6 0 8 12 2 2 4 4
[76] 6 2 4 0 0 4 4 2 2 0 2 2 0 4 4 2 0 2 2 2 8 10 0 2 2
$x4
[1] 6 10 4 6 6 12 8 6 2 4 4 8 6 6 2 6 8 4 6 6 6 8 2 10 6
[26] 6 2 8 8 4 6 8 10 4 6 8 10 10 4 2 4 4 4 6 14 4 6 2 4 6
[51] 0 14 0 10 8 10 0 4 6 6 4 12 4 8 12 2 8 4 6 2 4 10 10 8 8
[76] 2 4 2 6 6 2 4 2 4 6 2 6 8 4 8 10 0 4 6 4 0 4 6 2 8

Example2

 Live Demo

List2<−list(y1=rnorm(60),y2=rnorm(60),y3=rnorm(60),y4=rnorm(60))
List2

Output

$y1
[1] −0.31163597 1.31111498 −0.19732872 0.03670371 −1.74136270 −1.73202546
[7] −0.85604459 −1.08738522 0.04607248 0.23118367 −0.30069029 1.43418146
[13] 0.77450898 0.07784119 −0.43203884 0.13325803 −0.14362337 0.25831355
[19] 1.38866227 −1.09862061 0.64613438 −1.32020851 2.60029183 −0.39076198
[25] 1.95940690 −2.90627199 −0.85716448 −2.29113101 −0.36391797 0.27953465
[31] −0.55970366 1.66004351 0.37252545 −1.20711293 −0.97442206 −0.60294787
[37] 0.28482319 −0.47526746 −0.14472626 −0.34765058 −0.17956233 0.83179604
[43] −0.54103465 −0.59289760 −0.47182615 1.23503752 −1.55850727 −1.47658115
[49] 0.45597554 −1.88919278 −0.54725354 −0.12290071 −0.39011417 1.75596002
[55] 0.27345890 0.73473652 2.14774838 0.10433414 0.80609900 −0.82809754
$y2
[1] −1.623672143 0.003084120 −0.561001042 −0.178228838 −0.823175428
[6] 0.929877416 0.863926212 1.325543440 −0.541493024 −0.009362746
[11] −0.129437573 −1.585157629 0.877743659 −0.627391935 −0.654981581
[16] −0.306416266 0.558174323 −1.043557130 −2.573204941 −1.294325222
[21] −0.016823428 −1.368899397 −0.590039700 −2.202028385 −0.554894422
[26] 0.610074368 −0.819197524 −0.445224730 −1.900221008 0.601714463
[31] −0.079940702 0.694214712 1.316136858 0.037047671 −0.091877586
[36] −0.465346207 0.757206965 −0.378488738 0.122390473 −0.883163844
[41] −1.066922165 −0.270249611 −1.013880453 1.200566623 −0.339003531
[46] −0.047016170 −0.362101913 0.530307469 −0.130723097 0.007871984
[51] −0.349658650 −0.880650261 −0.908030270 −0.277984503 −0.493396224
[56] −0.426178105 −1.121039154 −0.315448483 −0.909254190 −1.603703109
$y3
[1] −0.88039331 0.76544832 1.22880981 0.16976841 1.12925426 0.53261916
[7] 0.28677249 1.70873367 −0.67911957 −1.19809637 0.69498197 1.19278318
[13] −0.05228474 −1.08885299 0.19005880 2.34425462 −1.57071722 0.72814587
[19] 2.11903192 0.65422408 −1.39110194 −1.14576983 0.27405209 −2.29361953
[25] −0.20552462 0.88146995 1.25313309 −0.20630170 0.73259536 −1.84433751
[31] −0.48475525 0.41909993 −0.07445892 −0.43277142 −1.64111889 −0.28010780
[37] 0.21812110 0.73271307 0.53579162 −2.33502312 0.23435896 −0.33286180
[43] −0.51595049 0.50167700 1.19512552 0.81918189 0.92670118 −1.14790393
[49] 0.52236941 0.43611326 −1.66171008 1.23703794 1.21188380 0.05098244
[55] −0.11015191 1.29874013 0.82369746 0.51228221 −0.82378145 −0.03410876
$y4
[1] −0.90671186 −1.62314816 0.53747830 −0.79900645 −0.48835823 1.30813592
[7] −3.00377348 1.08945104 −3.23377322 0.76212670 2.05818852 0.83020833
[13] −0.36424022 0.51441704 −1.19970463 −0.29484763 2.32675699 −0.38035400
[19] −0.14898653 0.05102723 1.62873977 0.56577113 0.58290260 −0.01132887
[25] −0.35880198 0.75001388 1.61442786 −1.19526377 −0.32975458 0.66136658
[31] −1.78432711 1.34315562 −0.49093017 1.15765222 0.70897553 −0.59435317
[37] −1.30037062 −0.32505505 1.24492762 0.38108942 1.80967268 −1.33037559
[43] 0.59490582 0.95420870 −0.85584273 0.67958732 0.46651930 0.15742018
[49] 1.05988736 0.64473740 −0.16923553 −1.59558728 0.17408084 −0.30705672
[55] 0.85890141 0.06370434 −1.33648832 −0.39414900 1.98460864 −0.77319788

Example

lapply(List2,"*",2)

Output

$y1
[1] −0.62327194 2.62222997 −0.39465744 0.07340742 −3.48272540 −3.46405093
[7] −1.71208919 −2.17477044 0.09214496 0.46236734 −0.60138058 2.86836291
[13] 1.54901796 0.15568238 −0.86407768 0.26651607 −0.28724674 0.51662711
[19] 2.77732454 −2.19724123 1.29226875 −2.64041702 5.20058366 −0.78152396
[25] 3.91881381 −5.81254398 −1.71432897 −4.58226201 −0.72783594 0.55906930
[31] −1.11940731 3.32008702 0.74505091 −2.41422586 −1.94884412 −1.20589575
[37] 0.56964637 −0.95053491 −0.28945252 −0.69530117 −0.35912465 1.66359207
[43] −1.08206931 −1.18579520 −0.94365230 2.47007503 −3.11701454 −2.95316230
[49] 0.91195108 −3.77838556 −1.09450709 −0.24580141 −0.78022833 3.51192004
[55] 0.54691780 1.46947305 4.29549677 0.20866829 1.61219799 −1.65619509
$y2
[1] −3.24734429 0.00616824 −1.12200208 −0.35645768 −1.64635086 1.85975483
[7] 1.72785242 2.65108688 −1.08298605 −0.01872549 −0.25887515 −3.17031526
[13] 1.75548732 −1.25478387 −1.30996316 −0.61283253 1.11634865 −2.08711426
[19] −5.14640988 −2.58865044 −0.03364686 −2.73779879 −1.18007940 −4.40405677
[25] −1.10978884 1.22014874 −1.63839505 −0.89044946 −3.80044202 1.20342893
[31] −0.15988140 1.38842942 2.63227372 0.07409534 −0.18375517 −0.93069241
[37] 1.51441393 −0.75697748 0.24478095 −1.76632769 −2.13384433 −0.54049922
[43] −2.02776091 2.40113325 −0.67800706 −0.09403234 −0.72420383 1.06061494
[49] −0.26144619 0.01574397 −0.69931730 −1.76130052 −1.81606054 −0.55596901
[55] −0.98679245 −0.85235621 −2.24207831 −0.63089697 −1.81850838 −3.20740622
$y3
[1] −1.76078661 1.53089665 2.45761962 0.33953682 2.25850851 1.06523833
[7] 0.57354499 3.41746734 −1.35823913 −2.39619274 1.38996394 2.38556636
[13] −0.10456948 −2.17770598 0.38011761 4.68850923 −3.14143444 1.45629174
[19] 4.23806383 1.30844816 −2.78220389 −2.29153965 0.54810418 −4.58723906
[25] −0.41104924 1.76293990 2.50626617 −0.41260340 1.46519071 −3.68867502
[31] −0.96951051 0.83819985 −0.14891784 −0.86554284 −3.28223777 −0.56021560
[37] 0.43624221 1.46542613 1.07158323 −4.67004624 0.46871793 −0.66572360
[43] −1.03190098 1.00335400 2.39025103 1.63836378 1.85340236 −2.29580786
[49] 1.04473881 0.87222652 −3.32342016 2.47407589 2.42376760 0.10196487
[55] −0.22030383 2.59748026 1.64739492 1.02456441 −1.64756290 −0.06821753
$y4
[1] −1.81342372 −3.24629631 1.07495661 −1.59801291 −0.97671646 2.61627183
[7] −6.00754697 2.17890207 −6.46754644 1.52425341 4.11637703 1.66041665
[13] −0.72848044 1.02883408 −2.39940926 −0.58969527 4.65351399 −0.76070800
[19] −0.29797306 0.10205446 3.25747953 1.13154225 1.16580519 −0.02265774
[25] −0.71760396 1.50002776 3.22885572 −2.39052753 −0.65950916 1.32273316
[31] −3.56865422 2.68631125 −0.98186034 2.31530444 1.41795107 −1.18870635
[37] −2.60074124 −0.65011010 2.48985524 0.76217885 3.61934536 −2.66075118
[43] 1.18981164 1.90841740 −1.71168547 1.35917465 0.93303860 0.31484035
[49] 2.11977472 1.28947481 −0.33847105 −3.19117456 0.34816169 −0.61411343
[55] 1.71780283 0.12740868 −2.67297665 −0.78829801 3.96921727 −1.54639577
raja
Published on 09-Feb-2021 16:41:33
Advertisements