1

I guess this is rather trivial. Given, e.g.,

L={1,2}

what are some common efficient ways to obtain

L
(*{1,2,1,2,1,2}*)

that is, the list concatenated three times with itself and obtained the new value. Thanks in advance.

Dimitris
  • 4,794
  • 22
  • 50

2 Answers2

4
L = {1, 2};
Charting`padList[L, 6]

{1,2,1,2,1,2}


Ps:Function Charting`CommonDump`listPad,Catenate,Flatten,ReplicateLayer,PaddingLayer,Table,ConstantArray,ArrayReshape and Array is relevant.Also ArrayPad from Kuba's comment here.

yode
  • 26,686
  • 4
  • 62
  • 167
4

There is a dedicated function in R:

Needs["RLink`"]    
InstallR[]    
REvaluate["rep(c(1,2),5)"]

(* {1., 2., 1., 2., 1., 2., 1., 2., 1., 2.} *)

Another possibility is to use length.out:

REvaluate["rep(c(1,2),length.out=5)"]    

(* {1., 2., 1., 2., 1.} *)
Anton Antonov
  • 37,787
  • 3
  • 100
  • 178