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