# Repeat with y as k

**URL:** <https://discourse.julialang.org/t/repeat-with-y-as-k/35771>\
**Category:** New to Julia\
**Created:** [March 9, 2020, 9:06pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771 "2020-03-09T21:06:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 9:06pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/1 "2020-03-09T21:06:11Z")

</div>

```julia
d = .05
n = .01
g = .015
s = .3
k=6
yn( k, s, d, g,) = k-(d+n+g)*k + s*k^(1/3)

```

I have this function, and it calculates yn= 6.09514 . Now I would like to replace k with yn, and find a new yn, and repeat that 50 times.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 9, 2020, 9:12pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/2 "2020-03-09T21:12:57Z")

</div>

Use a `for`-loop?

```julia
julia> function do_stuff(N)
           d = 0.05
           n = 0.01
           g = 0.015
           s = 0.3
           k = 6.0
           yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)
           for _ in 1:N
               k = yn(k, s, d, g)
           end
           return k
       end
do_stuff (generic function with 1 method)

julia> do_stuff(1)
6.095136177849642

julia> do_stuff(50)
7.839026641775517

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 9:20pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/3 "2020-03-09T21:20:29Z")

</div>

Thanks, that was easy, I’ll be sure to remember than in the future. Now I need to put each value of do\_stuff in a data frame, that shows each value of do\_stuff.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [March 9, 2020, 9:37pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/4 "2020-03-09T21:37:33Z")

</div>

```julia
function do_stuffdf(N)
    d = 0.05
    n = 0.01
    g = 0.015
    s = 0.3
    k = 6.0
    yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)

    df = DataFrame([Float64], [:k], 0)
    for _ in 1:N
        k = yn(k, s, d, g)
        push!(df, (k,))
    end
    return df
end

```

e.g.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 9:40pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/5 "2020-03-09T21:40:35Z")

</div>

> [@zweiglimmergneis](#):
>
> df = DataFrame([Float64], [:k], 0) for \_ in 1:N k = yn(k, s, d, g) push!(df, k) end return df end

it’s saying k not defined, not sure why, since I started with k=6 and keep repeating k.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 9:44pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/6 "2020-03-09T21:44:35Z")

</div>

Ok, yeah I put in the whole think, but it still only gives me one point.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [March 9, 2020, 9:44pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/7 "2020-03-09T21:44:54Z")

</div>

Oh, to get the DataFrame just define the function as posted and do:

```julia
mydataframe = do_stuffdf(50)

```

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [March 9, 2020, 9:47pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/8 "2020-03-09T21:47:51Z")

</div>

[There was a warning that you should not push Float64 into a DataFrame? I changed `push!(df, k)`to `push!(df, (k,))` and the warning is gone.]  
One point?

```julia
julia> do_stuffdf(50)
50×1 DataFrame
│ Row │ k │
│ │ Float64 │
├─────┼─────────┤
│ 1 │ 6.09514 │
│ 2 │ 6.186 │
│ 3 │ 6.27277 │
│ 4 │ 6.35558 │
│ 5 │ 6.43461 │
│ 6 │ 6.51001 │
│ 7 │ 6.58192 │
│ 8 │ 6.6505 │
⋮
│ 42 │ 7.7578 │
│ 43 │ 7.76985 │
│ 44 │ 7.7813 │
│ 45 │ 7.79218 │
│ 46 │ 7.80253 │
│ 47 │ 7.81236 │
│ 48 │ 7.8217 │
│ 49 │ 7.83059 │
│ 50 │ 7.83903 │

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 9:52pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/10 "2020-03-09T21:52:02Z")

</div>

Cool, work perfect, except that it starts with k =0, instead of k=6

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [March 9, 2020, 9:58pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/11 "2020-03-09T21:58:14Z")

</div>

The first `k` was missing. So this is an alternative, and `k` starts with 6.0

```julia
julia> function do_stuffdf(N)
           d = 0.05
           n = 0.01
           g = 0.015
           s = 0.3
           k = 6.0
           yn(k, s, d, g) = k - (d + n + g) * k + s * k^(1/3)

           df = DataFrame(k = k)
           for _ in 1:N
                   k = yn(k, s, d, g)
               push!(df, (k,))
           end
           return df
       end
do_stuffdf (generic function with 1 method)

julia> do_stuffdf(50)
51×1 DataFrame
│ Row │ k │
│ │ Float64 │
├─────┼─────────┤
│ 1 │ 6.0 │
│ 2 │ 6.09514 │
│ 3 │ 6.186 │
│ 4 │ 6.27277 │
│ 5 │ 6.35558 │
│ 6 │ 6.43461 │
│ 7 │ 6.51001 │
│ 8 │ 6.58192 │
⋮
│ 43 │ 7.7578 │
│ 44 │ 7.76985 │
│ 45 │ 7.7813 │
│ 46 │ 7.79218 │
│ 47 │ 7.80253 │
│ 48 │ 7.81236 │
│ 49 │ 7.8217 │
│ 50 │ 7.83059 │
│ 51 │ 7.83903 │

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [March 9, 2020, 10:06pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/12 "2020-03-09T22:06:13Z")

</div>

AWESOME!!! That’s it.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [March 10, 2020, 10:30pm UTC](https://discourse.julialang.org/t/repeat-with-y-as-k/35771/13 "2020-03-10T22:30:44Z")

</div>

If you want to create the DataFrame at top-level, you can put `k` in the context of the definition of `yn`:

```julia
julia> let k = 6.0
           global yn, setk
           function yn()
               d = 0.05
               n = 0.01
               g = 0.015
               s = 0.3
               r = k
               k = k - (d + n + g) * k + s * k^(1/3)
               return r
           end

           setk(kinit) = (k = kinit)
       end
setk (generic function with 1 method)

julia> setk(6.0)
6.0

julia> dfyn = DataFrame(yn = [yn() for i in 1:50])
50×1 DataFrame
│ Row │ yn │
│ │ Float64 │
├─────┼─────────┤
│ 1 │ 6.0 │
│ 2 │ 6.09514 │
│ 3 │ 6.186 │
│ 4 │ 6.27277 │
│ 5 │ 6.35558 │
│ 6 │ 6.43461 │
│ 7 │ 6.51001 │
│ 8 │ 6.58192 │
⋮
│ 42 │ 7.74512 │
│ 43 │ 7.7578 │
│ 44 │ 7.76985 │
│ 45 │ 7.7813 │
│ 46 │ 7.79218 │
│ 47 │ 7.80253 │
│ 48 │ 7.81236 │
│ 49 │ 7.8217 │
│ 50 │ 7.83059 │

```
