Filling vectors with repeated numbers

I am making a dataframe, but I want to repeat numbers as I add them.
I can use this code to make a table with the formulations:

df = map(i -> DataFrame(form = i, time = 0:1:3), 1:2)
df_vcat = vcat(df...)

But this only adds the form variable when I also want to add the rep variable to create a final dataframe like this:


I understand x:x:x format, but is there a way to fill with repeated numbers for a certain length?

You want multiple repeats for multiple formulations, so you will have to do a nested map:

times = 0:1:3

df = map(1:2) do form
    map(1:3) do rep
        DataFrame(form = form, rep = rep, time = times)
    end
end

df_vcat = vcat(vcat(df...)...)
1 Like

I think the better way to do this is with a crossjoin

julia> crossjoin(DataFrame(form=1:2), DataFrame(rep=1:3), DataFrame(time=0.0:3))
24×3 DataFrame
 Row │ form   rep    time
     │ Int64  Int64  Float64
─────┼───────────────────────
   1 │     1      1      0.0
   2 │     1      1      1.0
   3 │     1      1      2.0
   4 │     1      1      3.0
   5 │     1      2      0.0
   6 │     1      2      1.0
   7 │     1      2      2.0
   8 │     1      2      3.0
   9 │     1      3      0.0
  10 │     1      3      1.0
  11 │     1      3      2.0
  12 │     1      3      3.0
  13 │     2      1      0.0
  14 │     2      1      1.0
  15 │     2      1      2.0
  16 │     2      1      3.0
  17 │     2      2      0.0
  18 │     2      2      1.0
  19 │     2      2      2.0
  20 │     2      2      3.0
  21 │     2      3      0.0
  22 │     2      3      1.0
  23 │     2      3      2.0
  24 │     2      3      3.0
1 Like