Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store results from loop in Julia

Suppose that I have a function that makes some calculation and return a dataframe in Julia

using DataFrames, Random
function dummy_df(;theta)
    A = randn(100); B = randn(100); 
    A = A .* theta; B = B .* theta
    df_ = DataFrame(A = A, B = B)

    return(df_)
end

a = dummy_df(theta = 1)

Now suppose I have this values for theta.

tau = range(0.85, 1, length = 10)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I want to create some object that contains the dataframes produced for each element in tau vector. (In R it would be a list)

I think this is like a list from R

a = []

So I want to fill that list with all the dataframes.

So I am trying to do a for loop that runs dummy_df and stores that dataframe like this:

for (i,t) in pairs(tau)
    a[i] = dummy_df(theta = t)
end

But I have this error:

ERROR: BoundsError: attempt to access 0-element Vector{Any} at index [1]

>Solution :

The syntax to add items to an empty list is push!, using a[i] will just try to access an empty list that doesn’t have any items.

I also replaced your tau with t because I’m guessing you want the theta to change every loop not feed tau into the dummy_df.

I managed to create a list of dataframe with no errors.

using DataFrames, Random
function dummy_df(;theta)
    A = randn(100); B = randn(100); 
    A = A .* theta; B = B .* theta
    df_ = DataFrame(A = A, B = B)

    return(df_)
end

tau = range(0.85, 1, length = 10)
a = []
for (i,t) in pairs(tau)
    push!(a, dummy_df(theta = t)) #guessing you made a mistake here and meant to put t instead of tau
end

A printout of a looks like this,

10-element Vector{Any}:
 100×2 DataFrame
 Row │ A            B           
     │ Float64      Float64     
─────┼──────────────────────────
   1 │  0.958768    -0.302111
   2 │  0.370959     1.12717
   3 │  1.69035      1.45085
   4 │ -0.586892     0.258312
   5 │ -1.43566      1.01867
   6 │  0.0389977    0.471484
   7 │ -0.314276    -0.347831
   8 │ -0.00176065   0.494805
  ⋮  │      ⋮            ⋮
  94 │  0.700048    -1.50753
  95 │ -0.154113     1.04487
  96 │ -0.716135    -0.883592
  97 │  0.487718    -0.0188041
  98 │  1.01621      0.863817
  99 │ -0.578018     0.270933
 100 │  1.89336     -0.837134
                 85 rows omitted
 100×2 DataFrame
 Row │ A           B           
     │ Float64     Float64     
─────┼─────────────────────────
   1 │  0.388522   -1.18676
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading