# Julia equivalent to R's rep(..., length.out)

**URL:** <https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005>\
**Category:** New to Julia\
**Created:** [July 30, 2020, 7:56pm UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005 "2020-07-30T19:56:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kai](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kai](https://discourse.julialang.org/u/kai)\
**Post date:** [July 30, 2020, 7:56pm UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/1 "2020-07-30T19:56:50Z")

</div>

Copy-pasting a slack discussion for people who might need this:

**Question** :  
Can we add a `length_out` feature to the `repeat` function?  
In R, I can do `rep(1:3, length.out = 4)`, which gives `1 2 3 1` . It would be cool if `repeat` can do that.

**Answer given by Alex Arslan** :  
You can do something like

```julia
using Base.Iterators
collect(take(flatten((repeated(1:3))), 4))

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [July 30, 2020, 9:29pm UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/2 "2020-07-30T21:29:25Z")

</div>

You can drop the `flatten` if you use `cycle` instead of `repeated`:

```julia
collect(Iterators.take(Iterators.cycle(1:3), 4))

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [July 31, 2020, 12:33am UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/3 "2020-07-31T00:33:19Z")

</div>

How would this work for multiple dimensions?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [July 31, 2020, 1:11am UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/4 "2020-07-31T01:11:50Z")

</div>

almost sure that cyclying behaviour is 1D only. All higher D get mapped to 1D and then cycled.

---

<div class="post-metadata">

**Author:** ![mwsohn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mwsohn/32/2696_2.png) [@mwsohn](https://discourse.julialang.org/u/mwsohn)\
**Post date:** [July 31, 2020, 6:16am UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/5 "2020-07-31T06:16:11Z")

</div>

I would do it this way:

```julia
a = repeat(1:3,2)[1:4]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 31, 2020, 7:48am UTC](https://discourse.julialang.org/t/julia-equivalent-to-rs-rep-length-out/44005/6 "2020-07-31T07:48:47Z")

</div>

> [@kai](#):
>
> It would be cool if `repeat` can do that.

I disagree. I think that R’s “let’s try to recycle elements until needed” approach is a misfeature.

The solutions in this topic work fine when needed, but actually it is a very rare use case, and can hide length mismatch bugs (like pretty much all of R’s “recycling” semantics).
